Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if fixed width integers are defined

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?

like image 630
Rick de Water Avatar asked Jan 01 '20 12:01

Rick de Water


People also ask

What is a fixed width integer?

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.

What is int64_t data type in C++?

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.

Which of the following data types are standard integer types defined in the Stdint H header file?

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.


2 Answers

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.

like image 117
Nicol Bolas Avatar answered Sep 20 '22 23:09

Nicol Bolas


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 that typedef name and define the associated macros. Conversely, for each type described herein that the implementation does not provide, <stdint.h> shall not declare that typedef 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.

like image 42
Andrew Henle Avatar answered Sep 19 '22 23:09

Andrew Henle