c++ - 如何检查是否定义了固定宽度整数

标签 c++ c++11 types stdint cstdint

在 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 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++ 通过 <cstdint> 继承 C 实现。请参阅 <cstdint> vs <stdint.h> 了解一些细节。另请参阅What do __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS mean?详细信息__STDC_LIMIT_MACROS .

因此,如果 int32_t可用,INT32_MAXINT32_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/

相关文章:

c++ - 哈希输出为 BYTE 而不是 std::string?

c++ - 帮助阵列

c++ - 具有结构错误值的 std::map

python - 更改类型(pandas dataframe)时如何处理 'invalid literal for int() with base 10'?

class - 如何在Scala构造函数中声明类型别名?

android - 使用 Cygwin 创建 .SO 文件时出错

c++ - 'islower' 的无效重载

c++ - 如何避免在 C++ 中进行一些繁重的处理时阻塞线程?

c++ - C++11 std::function 是否限制函数指针可以具有的参数数量?

c - size_t 迭代的可变尺度