无符号长的类型与Windows(VS2010)上的uint32_t和uint64_t不同
Type of unsigned long is different from uint32_t and uint64_t on Windows (VS2010)
在Windows 7下的Visual Studio 2010上,32位无符号长似乎是与uint32_t和uint64_t不同的类型。参见以下测试程序:
#include <stdint.h>
#include <stdio.h>
template<class T, class U>
struct is_same_type
{
static const bool value = false;
};
template<class T>
struct is_same_type<T, T>
{
static const bool value = true;
};
#define TO_STRING(arg) TO_STRING_IMPL(arg)
#define TO_STRING_IMPL(arg) #arg
#define PRINT_SAME_TYPE(type1, type2) printf("%s (size=%d) %s %s (size=%d)n",
TO_STRING(type1), int(sizeof(type1)),
is_same_type<type1, type2>::value ? "==" : "!=",
TO_STRING(type2), int(sizeof(type2)))
int main(int /*argc*/, const char* /*argv*/[])
{
PRINT_SAME_TYPE(uint32_t, unsigned long);
PRINT_SAME_TYPE(uint64_t, unsigned long);
return 0;
}
我希望它能打印
uint32_t (size=4) != unsigned long (size=8)
uint64_t (size=8) == unsigned long (size=8)
(我在x86_64 Linux上获得)或
uint32_t (size=4) == unsigned long (size=4)
uint64_t (size=8) != unsigned long (size=4)
当然,假设该长度不长于64位。
然而,在Windows上,我得到了令人困惑的
uint32_t (size=4) != unsigned long (size=4)
uint64_t (size=8) != unsigned long (size=4)
这意味着存在两种不同的32位无符号类型。C++标准允许这样做吗?或者这是Visual C++编译器中的一个错误?
有两种不同的32位无符号类型
是的,有。CCD_ 1和CCD_。
C++标准允许这样做吗?
是的。规范规定(C++11§3.9.1[基本.基本]/2):
有五种标准的有符号整数类型:
signed char
、short int
、int
、long int
和long long int
。在该列表中,每种类型提供的存储量至少与列表中其前面的类型一样多对于每个标准的有符号整数类型,都存在一个对应的(但不同的)标准无符号整数类型。。。每个占用相同的存储量,并且具有与对应的带符号整数类型相同的对准要求
请注意,尽管int
和long
由相同数量的比特表示,但它们仍然是不同的类型(因此,例如,在过载解析期间,它们被不同地对待)。
相关文章:
- Linux的Cpp上的计时器
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 物理键盘上的触发器按键
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- SSL上的`curl_easy_send`和`curl_asy_recv`:如何处理`CURLE_AGAIN`
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 集合上的输出迭代器:assign和increment迭代器
- Cuda C++:设备上的Malloc类,并用来自主机的数据填充它
- 为什么我在leetcode上收到AddressSanitizer:地址0x602000000058上的堆缓冲区溢出错误
- 对于set上的循环-获取next元素迭代器
- 如何从棋盘上的箱号中找到行和列
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- if数组上的随机数
- 向量上的线性搜索
- 位阵列上的快速AND运算
- 无法将 GLFW 库与 Ubuntu 18.04 上的头文件链接
- 为什么一个向量上的多线程操作很慢
- 64位机器上的C++内存对齐
- 跟踪滚动条上的鼠标事件