In C++, fixed width integers are defined as optional, but I can't seems to find the recommended way to check if they are actually defined.
What would be a portable way to check if fixed width integers are available?
The fixed-width integer types that <inttypes. h> provides, include signed integer types, such as int8_t, int16_t, int32_t, int64_t, and unsigned integer types, such as uint8_t, uint16_t, uint32_t, and uint64_t.
A long on some systems is 32 bits (same as an integer), the int64_t is defined as a 64 bit integer on all systems (otherwise known as a long long). Portability may be affected using long, but using int64_t looks like it was created to increase portability.
h header defines integer types, limits of specified width integer types, limits of other integer types and macros for integer constant expressions. Note: For the exact width integer types, minimum width integer types and limits of specified width integer types we support bit sizes N with the values 8, 16, 32, and 64.
Broadly speaking... you don't.
If you need to use the fixed-sized integer types, then that means that you explicitly need those types to be of their specific sizes. That is, your code will be non-functional if you cannot get integers of those sizes. So you should just use them; if someone uses your code on a compiler that lacks said types, then your code will not compile. Which is fine, because your code wouldn't have worked if it did compile.
If you don't actually need fixed-sized integers but simply want them for some other reason, then use the int_least_*
types. If the implementation can give you exactly that size, then the least_*
types will have that size.
To determine if a fixed-width integer type is provided, you can check if either of the corresponding [U]INT*_MAX
or [U]INT*_MIN
macros is defined.
// may be necessary for your C++ implementation #define __STDC_LIMIT_MACROS #include <cstdint> #ifdef INT32_MAX // int32_t must be available to get here int32_t some32bitIntVariable; #endif
Per 7.20 Integer types <stdint.h>
, paragraph 4 of the C11 standard (note the bolded parts):
For each type described herein that the implementation provides,
<stdint.h>
shall declare thattypedef
name and define the associated macros. Conversely, for each type described herein that the implementation does not provide,<stdint.h>
shall not declare thattypedef
name nor shall it define the associated macros.
C++ inherits the C implementation via <cstdint>
. See <cstdint>
vs <stdint.h>
for some details. Also see What do __STDC_LIMIT_MACROS
and __STDC_CONSTANT_MACROS
mean? for details on __STDC_LIMIT_MACROS
.
Thus, if int32_t
is available, INT32_MAX
and INT32_MIN
must be #define
'd. Conversely, if int32_t
is not available, neither INT32_MAX
nor INT32_MIN
are allowed to be #define
'd.
Note though, as @NicolBolas stated in another answer, it may not be necessary to actually check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With