C ++ 11 data confusion

I'm trying to write a solid summary for C ++ data types, but I have some confusion about the new data types.

As I understood from my readings about C ++ data types, char16_t

both char_32_t

are fundamental data types and part of the mainstream language since C ++ 11.

It is mentioned that they represent different types of data.

Q1: What does "excellent" mean here?

Q2: Why was the type of intxx_t

type type int32_t

selected as a different base data type? And how can they be helpful in choosing them instead int

?

+2


source to share


4 answers


To answer the second part of the question:

Fixed-size integer types inherit from C, where they are typedef

s. It was decided to keep them typedef

for compatibility purposes. Note that C has no overloaded functions, so the need for "different" types is there below.

One of the reasons for using it int32_t

is that you need one or more of the required properties:



A signed integer type that is exactly 32 bits wide, with no padding bits, and using 2's complement for negative values.

If you use int

it can, should be 36 bits and use 1's complement.

However, unless you have special requirements, using a regular one int

will work fine. One advantage is that it int

will be available to all systems, and a 36-bit machine (or 24-bit embedded processor) may not have at all int32_t

.

+4


source


The types charXX_t

were introduced in N2249 . They are created as a separate type from uintXX_t

to provide overloading:



Define char16_t

as a separate new type that has the same size and appearance as uint_least16_t

. Likewise, define char32_t

as a separate new type that has the same size and appearance as uint_least32_t

.

[N1040 identified char16_t

and char32_t

how typedefs for uint_least16_t

and uint_least32_t

, that makes it impossible to overload these characters. ]

+4


source


To answer Q1:

Certain type means std::is_same<char16_t,uint_least16_t>::value

equal false

.

Overloaded functions are possible.

(However, there is no difference in size, caption, and alignment.)

+1


source


Another way to express "separate types" is that you can create two overloaded functions for each type. For example:

typedef int Int;
void f(int) { impl_1; }
void f(Int) { impl_2; }

      

If you try to compile a piece of code that contains both functions, the compiler will complain about an ODR violation: you are trying to override the same function twice, because their arguments are the same. This is because it typedef

does not create types, only aliases.

However, when the types are indeed different, both versions will be treated by the compiler as two different overloads.

+1


source







All Articles