The one idea
An arithmetic expression in C++ never operates on the types you wrote. Before a binary operator such as +, < or * does anything, two conversions run. First, every operand narrower than int is promoted, usually to int. Then the usual arithmetic conversions bring both operands to a single common type, chosen by a fixed ladder of rules. The operation happens in that common type, and the result has that type.
The standard does not tell you how wide most of these types are. It gives minimum widths, an ordering, and since C++20 a fixed representation for signed integers. Everything else, including whether plain char is signed, is the implementation's choice.
What the standard guarantees about the integer types
There are five standard signed integer types, signed char, short, int, long and long long, each with an unsigned counterpart of the same size. Since C++20, [basic.fundamental] states minimum widths for them: 8, 16, 16, 32 and 64 bits. Each type in the list has at least the range of the one before it. sizeof(char) is 1 by definition, and a byte has at least 8 bits, but it can have more.
That is the whole guarantee. int is 32 bits on every mainstream desktop platform, but 16 bits is conforming. long is 64 bits on 64-bit Linux and macOS (the LP64 model) and 32 bits on 64-bit Windows (LLP64). Code that assumes long holds a pointer is not portable.
C++20 adopted P1236, the wording for P0907: signed integers are two's complement. A signed type of width N holds exactly the values from -2^(N-1) to 2^(N-1)-1, and the same bit pattern is shared with the unsigned type for non-negative values. Two consequences follow. Converting an out-of-range value to a signed type is now well-defined and wraps modulo 2^N, where before C++20 the result was implementation-defined. Signed overflow in arithmetic, however, is still undefined behavior; the integer-arithmetic-and-overflow topic covers that side.
#include <climits>
static_assert(static_cast<signed char>(200) == -56); // guaranteed since C++20
static_assert(static_cast<unsigned>(-1) == UINT_MAX); // modular, always waschar is three types
char, signed char and unsigned char are three distinct types, even though char has the same representation and range as one of the other two. Which one is implementation-defined. On x86-64 Linux and on Apple's arm64 platforms, plain char is signed; on arm64 Linux it is unsigned. GCC and Clang let you flip it with -funsigned-char, which is a fine way to find code that depends on it.
#include <type_traits>
static_assert(!std::is_same_v<char, signed char>); // always distinct
static_assert(!std::is_same_v<char, unsigned char>); // also distinct
bool high_bit(char c) { return c < 0; } // true for '\xff' only if char is signedThe character types char8_t, char16_t, char32_t and wchar_t are distinct types too, each with an integer type as its underlying type. Their encoding questions belong to the character-types-and-unicode topic; here they matter only for how they promote.
Integral promotion
[conv.prom] turns every narrow integer operand into something at least as wide as int before arithmetic. The rules are:
- An integer type whose conversion rank is below
int's, other thanbooland the character types listed below, becomesintifintcan hold all its values, otherwiseunsigned int. This coverschar,signed char,unsigned char,shortandunsigned short. boolbecomesint, withfalseas 0 andtrueas 1.wchar_t,char8_t,char16_tandchar32_tbecome the first ofint,unsigned int,long,unsigned long,long longandunsigned long longthat can hold every value of their underlying type. With a 32-bitint,char32_ttherefore promotes tounsigned int.- An unscoped enumeration without a fixed underlying type promotes along the same list, starting from
int; one with a fixed underlying type promotes to that type, and then onward. - A bit-field promotes to
intifintcan hold all its values, otherwiseunsigned intif that can.
#include <cstdint>
#include <type_traits>
std::uint8_t a = 200, b = 200;
auto s = a + b; // int, value 400: no wraparound at 8 bits
static_assert(std::is_same_v<decltype(s), int>);
static_assert(std::is_same_v<decltype('a' + 'b'), int>);
static_assert(std::is_same_v<decltype(true + true), int>); // value 2Promotion is why unsigned short arithmetic can be undefined. If int is 32 bits, both operands of x * y become signed int, and 65535 times 65535 overflows a signed type.
Conversion rank and the usual arithmetic conversions
Every integer type has an integer conversion rank ([conv.rank]). long long outranks long, which outranks int, then short, then signed char. An unsigned type has the rank of its signed counterpart. char, signed char and unsigned char share a rank. bool ranks lowest of all. The character types have the rank of their underlying types, and an extended integer type ranks below a standard type of the same width.
[expr.arith.conv] then applies these steps in order, for operators such as +, -, *, /, %, the comparisons, the bitwise operators and the second and third operands of ?::
- If either operand has scoped enumeration type, no conversion is done, and the operands must have the same type.
- If either operand is floating, an integer operand converts to the floating type; of two floating types, the one of lesser floating-point conversion rank converts to the other, and unordered ranks make the expression ill-formed.
- Otherwise both operands are promoted, and if the types now match, that is the result.
- If both are signed or both unsigned, the one of lesser rank converts to the other.
- If the unsigned operand has rank greater than or equal to the signed one, the signed operand converts to the unsigned type.
- Otherwise, if the signed type can represent every value of the unsigned type, the unsigned operand converts to the signed type.
- Otherwise both convert to the unsigned counterpart of the signed type.
Steps 6 and 7 are where the platform leaks in. With LP64, long is wider than unsigned int, so step 6 applies and -1L < 1u is true. On LLP64, long and unsigned int are both 32 bits, step 7 applies, and the same comparison is false.
#include <type_traits>
static_assert(!(-1 < 0u)); // step 5: -1 becomes UINT_MAX
static_assert(std::is_same_v<decltype(1u + 1ll), long long>); // step 6 wherever long long
// is wider than unsigned int
// On LP64, long and unsigned long are both 64 bits:
// 1ul + 1ll has type unsigned long long (step 7)Mixing an unscoped enumeration with a floating type, or two different enumeration types, was deprecated in C++20 and is ill-formed in C++26.
Signed and unsigned comparison traps
Step 5 means any comparison between a negative int and an unsigned int or std::size_t is wrong. Compilers warn under -Wsign-compare, which -Wall enables in C++ for GCC. The common shapes are a container's size() compared with a signed index, v.size() - 1 on an empty vector wrapping to the maximum value, and a countdown loop over an unsigned index whose condition i >= 0 is always true.
#include <utility>
#include <vector>
bool safe(int i, const std::vector<int>& v) {
// return i < v.size(); // i == -1 converts to a huge value: false
return std::cmp_less(i, v.size()); // C++20: compares mathematical values
}C++20 added std::cmp_equal, std::cmp_less and the rest of the family in <utility>, which compare the mathematical values of any two standard integer types, and std::ssize, which returns a signed size.
bool, fixed-width types and extended floating types
bool. A boolean conversion ([conv.bool]) turns a zero value, a null pointer or a null member pointer into false and anything else into true; bool b = 0.5; is true. Going the other way, bool promotes to int. Incrementing a bool was removed in C++17, and decrementing it was never allowed.
Fixed-width integers. <cstdint> provides std::int8_t through std::int64_t and their unsigned counterparts. They are optional: they exist only if the implementation has a type of exactly that width with no padding bits. std::int_leastN_t and std::int_fastN_t for 8, 16, 32 and 64 are always present. std::uint8_t is almost always an alias for unsigned char, so it promotes to int and streams as a character.
Floating types. float, double and long double only promise that each has at least the precision of the one before; IEEE 754 is what implementations do, and std::numeric_limits<T>::is_iec559 reports it. C++23 (P1467) added optional extended floating-point types in <stdfloat>: std::float16_t, std::float32_t, std::float64_t, std::float128_t and std::bfloat16_t, with literal suffixes such as f16 and bf16. They are distinct from the standard types even when the format matches, and P1467 gave every floating type a floating-point conversion rank. A conversion from a higher rank to a lower one is no longer implicit when an extended type is involved.
#include <stdfloat>
#include <type_traits>
std::float16_t h = 1.0f16; // OK
// std::float16_t g = 1.0; // ill-formed: double has greater rank
static_assert(std::is_same_v<decltype(1.0f + 1.0f32), std::float32_t>);
// same rank as float, but the extended type has the greater subrankPrecision, rounding and special values are the floating-point-and-numerics topic's subject.
What to remember
- The standard fixes minimum widths (8, 16, 16, 32, 64 bits) and an ordering, not sizes; LP64 and LLP64 disagree about
long. - Since C++20 signed integers are two's complement and narrowing integer conversions wrap modulo 2^N; signed arithmetic overflow is still undefined.
char,signed charandunsigned charare distinct;char's signedness is implementation-defined.- Operands narrower than
int, includingbool,charandunsigned short, are promoted tointbefore arithmetic. - In mixed comparisons, an unsigned operand of equal or higher rank wins, so
-1 < 0uis false; usestd::cmp_lessandstd::ssize. - Whether a signed type absorbs an unsigned one depends on the platform's widths.
- Exact-width
std::intN_ttypes are optional; theleastandfastvariants are not. - C++23 extended floating types have their own conversion ranks and do not accept implicit narrowing from
double.