unsigned long的类型不同于Windows上的uint32_t和uint64_t(VS2010) [英] Type of unsigned long is different from uint32_t and uint64_t on Windows (VS2010)

查看:1760
本文介绍了unsigned long的类型不同于Windows上的uint32_t和uint64_t(VS2010)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2010在Windows 7,32位下,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)\\\
,\
TO_STRING(type1),int(sizeof(type1)),\
is_same_type& ,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

(我在x86_64 Linux上获得)或

  uint32_t(size = 4)== unsigned long(size = 4)
uint64_t(size = 8)!= unsigned long(size = 4)




$ b

>在Windows上,我得到了困惑

  uint32_t(size = 4)!= unsigned long(size = 4)
uint64_t(size = 8)!= unsigned long(size = 4)

是两个不同的32位无符号类型。这是C ++标准允许的吗?

解决方案


有两个不同的32位,unsigned types


是的,有。 int long 由32位表示。


这是C ++标准允许的吗?


是的。规范说明了(C ++ 11§3.9.1[basic.fundamental] / 2):


有五种标准的有符号整数类型: signed char short int int long int long long int 。在此列表中,每种类型至少提供与列表中之前相同的存储。



对于每个标准的带符号整数类型,存在相应的(但不同的)标准无符号整数类型...,每个类型占用相同的存储量并且具有与对应的有符号整数类型相同的对齐要求


请注意,尽管 int long 位数,它们仍然是不同的类型(因此,例如,它们在重载解析期间被不同地对待)。


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:

#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

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.

On Windows however, I get the baffling

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?

解决方案

There are two distinct 32-bit, unsigned types

Yes, there are. Both int and long are represented by 32 bits.

Is this allowed by the C++ standard?

Yes. The specification states (C++11 §3.9.1[basic.fundamental]/2):

There are five standard signed integer types : signed char, short int, int, long int, and long 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

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).

这篇关于unsigned long的类型不同于Windows上的uint32_t和uint64_t(VS2010)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆