![](/img/trans.png)
[英]convert a long string of characters to uint32_t or uint64_t in c++
[英]Type of unsigned long is different from uint32_t and uint64_t on Windows (VS2010)
在Windows 7,32位下的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;
}
我希望它能打印出来
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位无符号类型
是的,有。 int
和long
都用32位表示。
这是否允许C ++标准?
是。 规范说明(C ++11§3.9.1[basic.fundamental] / 2):
有五种标准的有符号整数类型:
signed char
,short int
,int
,long int
和long long int
。 在此列表中, 每种类型至少提供与列表中前面的存储一样多的存储空间。对于每个标准有符号整数类型,存在相应的(但不同的)标准无符号整数类型...每个类型占用相同的存储量并且具有与对应的有符号整数类型相同的对齐要求
请注意,尽管int
和long
由相同的位数表示,但它们仍然是不同的类型(因此,例如,在重载解析期间它们的处理方式不同)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.