Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make_signed<unsigned long>::type is int?

I'm using Visual Studio 2010 and the following code confused me a bit:

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

x will be of type int, but I would have expected long. I know that int and long in VS10 are both 4-byte integers. But even if a signed long fits into an int, int for me is not the signed integer type corresponding to unsigned long. So my question: is this a bug/technical inaccuracy or do the specifications of the standard allow this result?

like image 623
DaBrain Avatar asked Jan 12 '14 21:01

DaBrain


People also ask

Is unsigned long an integer?

Long unsigned integer type. Capable of containing at least the [0, 4,294,967,295] range. Long long signed integer type. Capable of containing at least the [−9,223,372,036,854,775,807, +9,223,372,036,854,775,807] range.

Is unsigned int the same as unsigned long?

Unsigned int is only guaranteed to be able to hold the numbers between 0 and 65535 (inclusive), while unsigned long int is guaranteed to be able to hold the numbers between 0 and 4 294 967 295.

Is unsigned an int or short?

Some properties of the unsigned short int data type are: Being an unsigned data type, it can store only positive values. Takes a size of 16 bits. A maximum integer value that can be stored in an unsigned short int data type is typically 65535, around 216 – 1(but is compiler dependent).

How do you define unsigned long long?

An unsigned version of the long long data type. An unsigned long long occupies 8 bytes of memory; it stores an integer from 0 to 2^64-1, which is approximately 1.8×10^19 (18 quintillion, or 18 billion billion). A synonym for the unsigned long long type is uint64 .


1 Answers

C++11 20.9.7.3 [meta.trans.sign] describes 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; 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]; 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.

Requires: T shall be a (possibly cv-qualified) integral type or enumeration but not a bool type.

I would consider "the corresponding signed integer type" of unsigned long to be 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:

There are five standard signed integer types: “signed char”, “short int”, “int”, “long int”, and “long long int”. ...

and 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; 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.

like image 115
Casey Avatar answered Sep 28 '22 07:09

Casey