【发布时间】:2017-02-05 21:51:27
【问题描述】:
在Visual Studio 14 中,stdint.h 标头具有固定宽度整数类型的定义,但如果您真正查看那里的定义,它们只是委托给原语。定义如下:
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
那么,如果 stdint.h 所做的只是回退到原语,那么还有什么理由使用它?我也知道 Visual Studio 不只是在编译时替换这些定义,因为如果您尝试将 int8_t 打印到控制台,您将得到一个 Unicode 字符而不是数字,因为它实际上只是一个 signed char。
编辑
因为人们指出,他们在逻辑上没有其他任何东西可以定义,我认为我的问题需要重申。
为什么在 C++ 规范中声明它将具有 8、16、32 和 64 位固定长度的整数的标头将这些整数定义为根据定义可以是编译器想要的任何大小的类型(到用别人在另一个问题The compiler can decide that an int will a 71 bit number stored in a 128 bit memory space where the additional 57 bits are used to store the programmers girlfriends birthday.) 中所说的方式?
【问题讨论】:
-
typedef的代表还会回复什么?并在 C++ 中使用<cstdint>,因为<stdint.h>已弃用。 -
@DeiDei
stdint.h应该包含固定宽度的整数,因为编译器可以将原语定义为几乎任何东西。 -
是的,编译器将原语定义为适合平台大小,库负责使
stdint.h中的typedef正确。不要对 Visual C++ 做出太多判断,因为它仅适用于 Windows,并且其中的整数类型变化不大。 -
@DeiDei OS X 也有基本相同的定义。
-
@DeiDei 我选择使用
stdint.h而不是cstdint因为cstdint只是回退到stdint.h。
标签: c++ visual-c++ stdint