3 ms·
> 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
by josefx 6y ago
> 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.
- PaulDavisThe1st 6y agoI 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.
- josefx 6y ago> 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.
- young_unixer 6y agoThe 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.
- pjmlp 6y agoWhen targeting an 8 or 16 bit processor.
- josefx 6y agoWhile 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.