News for hackers. Stuff that matters.
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.