简体   繁体   English

make_signed <unsigned long> :: type是int?

[英]make_signed<unsigned long>::type is int?

I'm using Visual Studio 2010 and the following code confused me a bit: 我正在使用Visual Studio 2010,以下代码让我有点困惑:

#include<type_traits>
auto x = std::make_signed<unsigned long>::type();

x will be of type int, but I would have expected long. x将是int类型,但我预计会很长。 I know that int and long in VS10 are both 4-byte integers. 我知道VS10中的int和long都是4字节整数。 But even if a signed long fits into an int, int for me is not the signed integer type corresponding to unsigned long. 但即使有符号长整数适合int,int对我来说也不是对应于unsigned long的有符号整数类型。 So my question: is this a bug/technical inaccuracy or do the specifications of the standard allow this result? 所以我的问题是:这是一个错误/技术不准确或标准的规范是否允许这个结果?

C++11 20.9.7.3 [meta.trans.sign] describes make_signed : C ++ 11 20.9.7.3 [meta.trans.sign]描述了make_signed

If T names a (possibly cv-qualified) signed integer type (3.9.1) then the member typedef type shall name the type T ; 如果T命名一个(可能是cv限定的)有符号整数类型(3.9.1),那么成员typedef type应该命名类型T ; otherwise, if T names a (possibly cv-qualified) unsigned integer type then type shall name the corresponding signed integer type, with the same cv-qualifiers as T [ emphasis added ]; 否则, 如果T命名一个(可能是cv限定的)无符号整数类型,则type应命名相应的有符号整数类型,其中cv-qualifiers与T [ emphasis added ]相同; otherwise, type shall name the signed integer type with smallest rank (4.13) for which sizeof(T) == sizeof(type) , with the same cv-qualifiers as T . 否则, type应为具有最小等级(4.13)的有符号整数类型命名,其sizeof(T) == sizeof(type) ,具有与T相同的cv限定符。

Requires: T shall be a (possibly cv-qualified) integral type or enumeration but not a bool type. 要求: T应为(可能是cv限定的)整数类型或枚举,但不是bool类型。

I would consider "the corresponding signed integer type" of unsigned long to be long . 我会认为unsigned long “相应的有符号整数类型”很long I don't think there's much room for interpretation. 我认为没有太多的解释空间。

EDIT: There's no room for interpretation since the standard defines "corresponding signed integer type". 编辑:由于标准定义了“相应的有符号整数类型”,因此无法进行解释。 3.9.1/2 states: 3.9.1 / 2状态:

There are five standard signed integer types : “ signed char ”, “ short int ”, “ int ”, “ long int ”, and “ long long int ”. 有五种标准的有符号整数类型 :“ signed char ”,“ short int ”,“ int ”,“ long int ”和“ long long int ”。 ... ...

and 3.9.1/3: 和3.9.1 / 3:

For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type : “ unsigned char ”, “ unsigned short int ”, “ unsigned int ”, “ unsigned long int ”, and “ unsigned long long int ”, each of which occupies the same amount of storage and has the same alignment requirements (3.11) as the corresponding signed integer type; 对于每个标准有符号整数类型,存在相应的(但不同的) 标准无符号整数类型 :“ unsigned char ”,“ unsigned short int ”,“ unsigned int ”,“ unsigned long int ”和“ unsigned long long int “,每个存储占用相同的存储量,并且具有与相应的有符号整数类型相同的对齐要求(3.11); that is, each signed integer type has the same object representation as its corresponding unsigned integer type. 也就是说,每个有符号整数类型具有与其对应的无符号整数类型相同的对象表示。 ... ...

The corresponding signed integer type of unsigned long int is clearly long int . 相应的有符号整数类型的unsigned long int显然是long int

MAYBE your test might be flawed? 可能你的测试可能有缺陷? I'm not sure as you posted no code.. However, for such a test, DO NOT use the size to determine what type is being returned. 我不确定你没有发布任何代码..但是,对于这样的测试,请勿使用大小来确定返回的类型。 Such a test is inaccurate when a long can be same size as int. 当long可以与int相同时,这样的测试是不准确的。

For example: 例如:

#include <type_traits>
#include <iostream>
#include <typeinfo>

std::make_signed<unsigned long>::type x;

int main()
{
    std::cout<<(sizeof(x) == sizeof(int));
}

is implementation defined and can return true if the long is the same size as the int. 是实现定义的,如果long与int的大小相同,则返回true。 On most systems this will return true. 在大多数系统上,这将返回true。

Doing: 这样做:

#include <type_traits>
#include <iostream>
#include <typeinfo>

std::make_signed<unsigned long>::type x;

int main()
{
    std::cout<<typeid(x).name();
}

will print L if x is a long and it will print I if x is an int . 将打印L ,如果x是一个long而将打印I ,如果x是一个int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM