integral types and specifiers

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

integral types and specifiers

Post by albinopapa » November 8th, 2019, 5:32 am

Code: Select all

	static_assert( std::is_same_v<char, signed char> );     // failed
	static_assert( std::is_same_v<char, unsigned char> );   // failed

	static_assert(std::is_same_v<short, signed short>);     // passed
	static_assert(std::is_same_v<short, unsigned short>);   // failed

	static_assert(std::is_same_v<int, signed int>);         // passed
	static_assert(std::is_same_v<int, unsigned int>);       // failed

	static_assert(std::is_same_v<long, signed long>);       // passed
	static_assert(std::is_same_v<long, unsigned long>);     // failed

	static_assert(std::is_same_v<long, int>);               // failed
As you can see, comparing an integral type with it's signed specifier passes with the exception of char. Char seems to be an outlier among it's integral brethren and here's what the standard has to say:
C++ Standard wrote: Type char is a distinct type that has an implementation-defined choice of “signed char” or “unsigned char” as its underlying type. The values of type char can represent distinct codes for all members of the implementation's basic character set. The three types char, signed char, and unsigned char are collectively called ordinary character types. The ordinary character types and char8_­t are collectively called narrow character types. For narrow character types, each possible bit pattern of the object representation represents a distinct value. [ Note: This requirement does not hold for other types. — end note ] [ Note: A bit-field of narrow character type whose width is larger than the width of that type has padding bits; see [basic.types]. — end note ]
I was completely unaware of the face 'char' could swing either way depending on compiler implementation. I always took it to be the same as 'signed char'.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: integral types and specifiers

Post by chili » November 8th, 2019, 3:46 pm

Interesting. I never heard about this before either.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: integral types and specifiers

Post by albinopapa » November 8th, 2019, 9:09 pm

Either not many people have or it's suppose to be common knowledge, because I have never come across this in any C++ tutorials, video or text. I only came across this because I tried using int8_t* for a string and the compiler was like "Dafuq?". I thought is was a bug in VS, but I went to godbolt.org and sure enough, clang and gcc both said the same thing.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: integral types and specifiers

Post by chili » November 10th, 2019, 4:39 am

Godbolt is incredibly useful.
Chili

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: integral types and specifiers

Post by albinopapa » November 10th, 2019, 6:33 am

Yes, very. It's nice having other compilers telling you different error messages which might help diagnose issues.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply