News for hackers. Stuff that matters.
Re:A Special Kind of Hell: intmax_t in C and C++ by stefan_ on Saturday December 05, 2020 @03:31PM (#25318024)
It seems the original sin was having an intmax type in the first place, instead of only fixed-size types. And then of course the stupidity in using it: just because a high-end CPU can do math on 512 bit integers within some 10-20 cycles shouldn't mean that after a recompile all the chrono code is suddenly a huge register stomping inefficient mess, for no good fucking reason at all.

And on the other end of the spectrum, someone might well decide 16 bit is the intmax for an AVR8. Now your chrono code doesn't work at all.

Re:A Special Kind of Hell: intmax_t in C and C++ by PaulDavisThe1st on Saturday December 05, 2020 @03:36PM (#25318070)
This, this and more this.

intmax_t is as bad as int, for precisely the same reason. The developer has no idea what the capabilities of the type are. "Give me the biggest integer type you have" is not something any developer should say, ever, at least not in code that is intended to be remotely portable across time, space and architecture.

Re:A Special Kind of Hell: intmax_t in C and C++ by TwoBit on Saturday December 05, 2020 @04:47PM (#25318730)
The only type worse than 'int' is 'long'.
Re:A Special Kind of Hell: intmax_t in C and C++ by PaulDavisThe1st on Saturday December 05, 2020 @05:54PM (#25319227)
it's really a tie between "int", "long" and DWORD (though thankfully DWORD never made it into a language standard).

I would put "char" in the running now that we (thankfully) live in an increasingly UTF-8 world, but at least it's more or less universally understood to mean an 8 bit entity (even though this is not strictly true). It certainly isn't "a character".

Re:A Special Kind of Hell: intmax_t in C and C++ by mehrdadn on Sunday December 06, 2020 @03:38AM (#25322074)
long is definitely worse than int. At least you know int is never 64-bit on desktops, and it comes in handy enough for things like logarithms. long being either 32-bit or 64-bit on the same platform depending on the compiler just feels like insanity.
Re:A Special Kind of Hell: intmax_t in C and C++ by MaulingMonkey on Sunday December 06, 2020 @10:39AM (#25324171)
It's absolutely insanity. A cruel question I came up with the other day, based on it - What is the result of:

    std::is_signed<decltype(1L * 2U)>()

?
Re:A Special Kind of Hell: intmax_t in C and C++ by folmar on Sunday December 06, 2020 @02:14PM (#25325818)
Why should it be cruel? Promotions are a part of life of an integer in C.
Re:A Special Kind of Hell: intmax_t in C and C++ by MaulingMonkey on Sunday December 06, 2020 @07:35PM (#25328107)
Even programmers who are roughly aware of integral promotion rules may not have connected all the dots to realize the answer is implementation dependent.

If sizeof(long) == sizeof(unsigned int), 1L * 2U results in an unsigned int, meaning the result is "false".

If sizeof(long) > sizeof(unsigned int), 1L * 2U result in a signed long, meaning the result is "true".

Re:A Special Kind of Hell: intmax_t in C and C++ by mehrdadn on Sunday December 06, 2020 @09:16PM (#25328733)
Wow that is cruel! Somehow I just assumed int would get promoted to long regardless of their size difference. That this isn't the case almost seems like a language defect!
Re:A Special Kind of Hell: intmax_t in C and C++ by SloopJon on Saturday December 05, 2020 @04:25PM (#25318529)
I used intmax_t just yesterday in an ODBC type conversion test. I wrote a template for use in a Google Test typed test to find the highest and lowest values supported by the client and server type. Something like:

  max(
    static_cast<intmax_t>(
      numeric_limits<T>::lowest()),
    static_cast<intmax_t>(
      numeric_limits<U>::lowest()))
I tried std::common_type first, but it didn't quite do the trick.
Re:A Special Kind of Hell: intmax_t in C and C++ by secondcoming on Saturday December 05, 2020 @04:29PM (#25318570)
Out of curiosity... did you ever fall into the trap of using

    numeric_limits<T>::min()
instead of

    numeric_limits<T>::lowest()
? Because I did.
Re:A Special Kind of Hell: intmax_t in C and C++ by saagarjha on Saturday December 05, 2020 @04:35PM (#25318621)
The distinction is only relevant for floating point types, is it not?
Re:A Special Kind of Hell: intmax_t in C and C++ by secondcoming on Sunday December 06, 2020 @08:58AM (#25323478)
Yes, I believe so.
Re:A Special Kind of Hell: intmax_t in C and C++ by SloopJon on Sunday December 06, 2020 @01:36AM (#25321635)
Yeah, min() returns the most negative int, so it seems like it should do the same for float. Then the float with the least magnitude could have been, say, smallest().
Re:A Special Kind of Hell: intmax_t in C and C++ by josefx on Saturday December 05, 2020 @05:01PM (#25318838)
> at least not in code that is intended to be remotely portable across time, space and architecture.

intmax_t exists probably because most integer types in C are not portable. You can't write C code that uses int64_t and expect it to compile, the type doesn't have to exist, neither do int32_t or int16_t. Say you have a generic int lowestBitSet(intmax_t) function and want to make it available, you don't care how large the input integer is as long as you do not truncate the largest type available to whoever ends up using it. Without intmax_t you end up with library developers having to either setting their own upper limit, limiting the portability of their library or checking for the availability of each type themselves.

Re:A Special Kind of Hell: intmax_t in C and C++ by PaulDavisThe1st on Saturday December 05, 2020 @05:52PM (#25319205)
I don't use int64_t and expect it to exist. I use int64_t and expect compilation to fail if stdint.h doesn't define it. It would be a problem, but much less of a problem than using intWhatever_t, having compilation work, but having no idea what the bounds were.

intmax_t doesn't define an upper limit either, as TFA explains, because you don't know at runtime what the limit really is.

Re:A Special Kind of Hell: intmax_t in C and C++ by josefx on Sunday December 06, 2020 @04:00AM (#25322174)
> but having no idea what the bounds were.

The idea for the library writer are that the bounds are large enough for whatever the user needs. User inputs a int64_t? Great the type intmax_t on that system is at least int64_t wide by definition. System doesn't have a int64_t ? Great the user can at best use a int32_t, which intmax_t would once again support.

> intmax_t doesn't define an upper limit either, as TFA explains

Yeah, it would almost be sad that C++ is getting bitten by Cs lack of name mangling. Almost because it is kinda pointless to define a maximum integer in a language where any programmer can add their own fixed sized integer types, so a generic library supporting arbitrary sized integers would have to take template parameters anyway.

Re:A Special Kind of Hell: intmax_t in C and C++ by young_unixer on Sunday December 06, 2020 @12:28AM (#25321318)
The standard requires longs to be at least 32 bits wide. Then how can int32_t not exist?

And C99 requires long longs to be at least 64 bits wide.

Re:A Special Kind of Hell: intmax_t in C and C++ by josefx on Sunday December 06, 2020 @04:11AM (#25322226)
While that isn't really relevant as the sizes used by my example could just be larger (if intmax_t worked as intended, which of course it doesn't), the fixed width types are all optional. For example on systems that use ones complement you would have a intmax_t, but no int32_t or any of the others.
Re:A Special Kind of Hell: intmax_t in C and C++ by pjmlp on Sunday December 06, 2020 @01:58AM (#25321710)
When targeting an 8 or 16 bit processor.
Re:A Special Kind of Hell: intmax_t in C and C++ by oofabz on Saturday December 05, 2020 @04:10PM (#25318396)
>only fixed-size types

I agree that intmax is silly, but I believe there are legitimate uses for variable-size types, like int_fast8_t and uint_fast16_t. These are their nominal size on x86 because the architecture has arithmetic operations for those sizes. But on POWER, they are both 32-bit types, because there are no 8-bit or 16-bit opcodes, and emulating overflow behavior with bitmasks incurs a performance hit.

Re:A Special Kind of Hell: intmax_t in C and C++ by forrestthewoods on Saturday December 05, 2020 @04:32PM (#25318597)
Implementing this has the language level has proven to be a total disaster.

The number of cases where it’s important to use different types on different architectures exists, but is very small.

That’s something that would be much easier to handle at the library-level for a finite set of architectures than at the language level for all archs.

I’d also like to see the elimination of printf specifiers. Another total disaster and source of soooooo many bugs. Especially with all of these variable types.

Re:A Special Kind of Hell: intmax_t in C and C++ by kps on Saturday December 05, 2020 @03:54PM (#25318250)
C99, which introduced `intmax_t`, also requires that a compiler provide a `long long int` of at least 64 bits. So on the bright side, `intmax_t` will be at least 64 bits, and on the down side, you really don't want to use that type on an AVR.
Re:A Special Kind of Hell: intmax_t in C and C++ by loeg on Saturday December 05, 2020 @04:43PM (#25318692)
It's probably fine for addition on an AVR, but I'm having trouble imagining what you'd want to count that needs more than 32 bits on an AVR.
Re:A Special Kind of Hell: intmax_t in C and C++ by CamperBob2 on Saturday December 05, 2020 @10:51PM (#25320860)
Tick counts. It's nice to be able to do time-difference subtractions (and comparisons!) without worrying about rollover.

Heck, I use 64-bit ints on 8051 cores that make an AVR look like something out of Star Trek.

Re:A Special Kind of Hell: intmax_t in C and C++ by secondcoming on Saturday December 05, 2020 @04:27PM (#25318547)
Indeed. And look at all the uint_fast32_t, etc, stuff too:

https://stackoverflow.com/questions/8500677/what-is-uint-fas...

Re:A Special Kind of Hell: intmax_t in C and C++ by saagarjha on Saturday December 05, 2020 @04:34PM (#25318611)
But if you used a fixed size type, you end up doing things like using 64-bit integers on AVR, where you’re going to again have a bad time. Flexible and fixed width types both have their uses.
Re:A Special Kind of Hell: intmax_t in C and C++ by forrestthewoods on Saturday December 05, 2020 @04:28PM (#25318562)
Agreed 100%