Reputation: 1771
I am trying to write a solid summary for C++ datatypes, but I have some confusion about the new datatypes.
As I understood from my readings about C++ data types, char16_t
and char_32_t
are fundamental data types and part of the core language since C++11.
It is mentioned that they are distinct data types.
Q1: What exactly does "distinct" mean here?
Q2: Why intxx_t
type family like int32_t
was chosen not to be a fundamental datatype? And how can they be beneficial when choosing them instead of int
?
Upvotes: 2
Views: 643
Reputation: 12259
Other way to express "distinct types" is that you can create two overloaded functions for each type. For instance:
typedef int Int;
void f(int) { impl_1; }
void f(Int) { impl_2; }
If you try to compile a code snippet containing both functions, the compiler will complain about a ODR violation: you are trying to redefine the same function twice, since their arguments are the same. That's because typedef
s doesn't create types, just aliases.
However, when types are truly distinct, both versions will be seen as two different overloads by the compiler.
Upvotes: 1
Reputation: 92241
To answer the second part of the question:
The fixed size integer types are inherited from C, where they are typedef
s. It was decided to keep them as typedef
s to be compatible. Note that the C language doesn't have overloaded functions, so the need for "distinct" types is lower there.
One reason for using int32_t
is that you need one or more of its required properties:
Signed integer type with width of exactly 32 bits with no padding bits and using 2's complement for negative values.
If you use an int
it might, for example, be 36 bits and use 1's complement.
However, if you don't have very specific requirements, using a normal int
will work fine. One advantage is that an int
will be available on all systems, while the 36-bit machine (or a 24 bit embedded processor) might not have any int32_t
at all.
Upvotes: 4
Reputation: 5944
To answer your Q1:
Distinct type means std::is_same<char16_t,uint_least16_t>::value
is equal to false
.
So overloaded functions are possible.
(There is no difference in size, signedness, and alignment, though.)
Upvotes: 1
Reputation: 523224
The charXX_t
types were introduced in N2249. They are created as a distinct type from uintXX_t
to allow overloading:
Define
char16_t
to be a distinct new type, that has the same size and representation asuint_least16_t
. Likewise, definechar32_t
to be a distinct new type, that has the same size and representation asuint_least32_t
.[N1040 defined
char16_t
andchar32_t
as typedefs touint_least16_t
anduint_least32_t
, which make overloading on these characters impossible.]
Upvotes: 4