Does the standard guarantee that uint8_t, int8_t, and char are all unique types?

436 Views Asked by At

It seems the following is guaranteed to pass (asked already here):

#include <type_traits>
static_assert(!std::is_same_v<char, signed char>);
static_assert(!std::is_same_v<char, unsigned char>);

To quote cppreference

[char] has the same representation and alignment as either signed char or unsigned char, but is always a distinct type

Is it also guaranteed that int8_t and uint8_t are defined in terms of the explicitly signed types not defined in terms of char, and therefore also form a set of 3 distinct types with char?

#include <cstdint>
#include <type_traits>
static_assert(!std::is_same_v<char, int8_t>);
static_assert(!std::is_same_v<char, uint8_t>);
2

There are 2 best solutions below

0
Bathsheba On

On your first point, yes, char, signed char, and unsigned char must always be distinct types.

On the second point, int8_t and uint8_t might be or might not be the same type as a char (or its signed or unsigned variants); i.e. there is no guarantee for or against.

4
Remy Lebeau On

The fixed-width types are implementation-defined aliases. The (u)int8_t types are NOT guaranteed to be aliases for any of the fundamental char types at all. They are only guaranteed to be (un)signed 8-bit integer types. They MAY be aliases for (un)signed char, or they MAY be aliases for vendor-specific types, such as (un)signed __int8. It is up to each compiler vendor to decide what aliases best suits their implementation.