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?
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.
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.
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).
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 .
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 typedeftype
shall name the typeT
; otherwise, ifT
names a (possibly cv-qualified) unsigned integer type thentype
shall name the corresponding signed integer type, with the same cv-qualifiers asT
[emphasis added]; otherwise,type
shall name the signed integer type with smallest rank (4.13) for whichsizeof(T) == sizeof(type)
, with the same cv-qualifiers asT
.Requires:
T
shall be a (possibly cv-qualified) integral type or enumeration but not abool
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With