在 C++ 中,固定宽度整数定义为 optional ,但我似乎找不到推荐的方法来检查它们是否实际定义。
检查固定宽度整数是否可用的可移植方法是什么?
最佳答案
要确定是否提供了固定宽度整数类型,您可以检查是否有相应的[U]INT*_MAX
或[U]INT*_MIN
宏已定义。
// 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
每7.20 Integer types <stdint.h>
, paragraph 4 of the C11 standard (注意粗体部分):
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++ 通过 <cstdint>
继承 C 实现。请参阅 <cstdint>
vs <stdint.h>
了解一些细节。另请参阅What do __STDC_LIMIT_MACROS
and __STDC_CONSTANT_MACROS
mean?详细信息__STDC_LIMIT_MACROS
.
因此,如果 int32_t
可用,INT32_MAX
和INT32_MIN
必须是#define
'd。相反,如果int32_t
不可用,也不INT32_MAX
也不INT32_MIN
允许为#define
'd。
但请注意,as @NicolBolas stated in another answer ,可能不需要实际检查。
关于c++ - 如何检查是否定义了固定宽度整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59552571/