C++ unsigned long 的类型与 Windows (VS2010) 上的 uint32_t 和 uint64_t 不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11611467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type of unsigned long is different from uint32_t and uint64_t on Windows (VS2010)
提问by tbleher
On Visual Studio 2010 under Windows 7, 32bit, unsigned long seems to be a distinct type from both uint32_t and uint64_t. See the following test program:
在 32 位 Windows 7 下的 Visual Studio 2010 上,unsigned long 似乎是与 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;
}
I'd expect it to print either
我希望它可以打印
uint32_t (size=4) != unsigned long (size=8)
uint64_t (size=8) == unsigned long (size=8)
(which I get on x86_64 Linux) or
(我在 x86_64 Linux 上得到)或
uint32_t (size=4) == unsigned long (size=4)
uint64_t (size=8) != unsigned long (size=4)
assuming of course that long is not longer than 64bits.
当然,假设 long 不超过 64 位。
On Windows however, I get the baffling
然而,在 Windows 上,我感到莫名其妙
uint32_t (size=4) != unsigned long (size=4)
uint64_t (size=8) != unsigned long (size=4)
which means that there are two distinct 32bit unsigned types. Is this allowed by the C++ standard? Or is this a bug in the Visual C++ compiler?
这意味着有两种不同的 32 位无符号类型。C++ 标准允许这样做吗?或者这是 Visual C++ 编译器中的错误?
回答by James McNellis
There are two distinct 32-bit, unsigned types
有两种不同的 32 位无符号类型
Yes, there are. Both int
and long
are represented by 32 bits.
是的,有。二者int
并long
用32位表示。
Is this allowed by the C++ standard?
C++ 标准允许这样做吗?
Yes. The specification states (C++11 §3.9.1[basic.fundamental]/2):
是的。规范指出(C++11 §3.9.1[basic.fundamental]/2):
There are five standard signed integer types :
signed char
,short int
,int
,long int
, andlong long int
. In this list, each type provides at least as much storage as those preceding it in the list.For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type...each of which occupies the same amount of storage and has the same alignment requirements as the corresponding signed integer type
有五种标准符号整数类型:
signed char
,short int
,int
,long int
,和long long int
。在此列表中,每种类型至少提供与列表中它之前的类型一样多的存储空间。对于每个标准的有符号整数类型,都存在相应的(但不同的)标准无符号整数类型……每个类型都与相应的有符号整数类型占用相同的存储量和对齐要求
Note that despite the fact that int
and long
are represented by the same number of bits, they are still different types (so, for example, they are treated differently during overload resolution).
请注意,尽管事实上int
和long
由相同的位数表示,但它们仍然是不同的类型(因此,例如,在重载解析期间,它们的处理方式不同)。