21 KiB
[basic.fundamental]
6 Basics [basic]
6.9 Types [basic.types]
6.9.2 Fundamental types [basic.fundamental]
There are five standard signed integer types:âsigned charâ, âshort intâ, âintâ, âlong intâ, and âlong long intâ.
In this list, each type provides at least as much storage as those preceding it in the list.
There may also be implementation-definedextended signed integer types.
The standard and extended signed integer types are collectively calledsigned integer types.
The range of representable values for a signed integer type isâ2Nâ1 to 2Nâ1â1 (inclusive), where N is called the width of the type.
[Note 1:
Plain ints are intended to have the natural width suggested by the architecture of the execution environment; the other signed integer types are provided to meet special needs.
â end note]
For each of the standard signed integer types, there exists a corresponding (but different)standard unsigned integer type:âunsigned charâ, âunsigned short intâ, âunsigned intâ, âunsigned long intâ, and âunsigned long long intâ.
Likewise, for each of the extended signed integer types, there exists a corresponding extended unsigned integer type.
The standard and extended unsigned integer types are collectively called unsigned integer types.
An unsigned integer type has the same width N as the corresponding signed integer type.
The range of representable values for the unsigned type is0 to 2Nâ1 (inclusive); arithmetic for the unsigned type is performed modulo 2N.
[Note 2:
Unsigned arithmetic does not overflow.
Overflow for signed arithmetic yields undefined behavior ([expr.pre]).
â end note]
An unsigned integer type has the same object representation, value representation, and alignment requirements ([basic.align]) as the corresponding signed integer type.
For each value x of a signed integer type, the value of the corresponding unsigned integer type congruent to x modulo 2N has the same value of corresponding bits in its value representation.30
[Example 1:
The value â1 of a signed integer type has the same representation as the largest value of the corresponding unsigned type.
â end example]
Table 14 — Minimum width [tab:basic.fundamental.width]
| ð Type |
Minimum width N |
|---|---|
| ð signed char |
8 |
| ð short int |
16 |
| ð int |
16 |
| ð long int |
32 |
| ð long long int |
64 |
The width of each standard signed integer type shall not be less than the values specified in Table 14.
The value representation of a signed or unsigned integer type comprises N bits, where N is the respective width.
Each set of values for any padding bits ([basic.types.general]) in the object representation are alternative representations of the value specified by the value representation.
[Note 3:
Padding bits have unspecified value, but cannot cause traps.
In contrast, see ISO/IEC 9899:2024 6.2.6.2.
â end note]
[Note 4:
The signed and unsigned integer types satisfy the constraints given in ISO/IEC 9899:2024 5.2.4.2.1.
â end note]
Except as specified above, the width of a signed or unsigned integer type isimplementation-defined.
Each value x of an unsigned integer type with width N has a unique representation x=x020+x121+â¦+xNâ12Nâ1, where each coefficient xi is either 0 or 1; this is called the base-2 representation of x.
The base-2 representation of a value of signed integer type is the base-2 representation of the congruent value of the corresponding unsigned integer type.
The standard signed integer types and standard unsigned integer types are collectively called the standard integer types, and the extended signed integer types and extended unsigned integer types are collectively called theextended integer types.
A fundamental type specified to have a signed or unsigned integer type as its underlying type has the same object representation, value representation, alignment requirements ([basic.align]), and range of representable values as the underlying type.
Further, each value has the same representation in both types.
Type char is a distinct type that has an implementation-defined choice of âsigned charâ or âunsigned charâ as its underlying type.
The three types char, signed char, and unsigned char are collectively calledordinary 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 5:
This requirement does not hold for other types.
â end note]
[Note 6:
A bit-field of narrow character type whose width is larger than the width of that type has padding bits; see [basic.types.general].
â end note]
Type wchar_t is a distinct type that has an implementation-defined signed or unsigned integer type as its underlying type.
Type char8_t denotes a distinct type whose underlying type is unsigned char.
Types char16_t and char32_t denote distinct types whose underlying types are uint_least16_t and uint_least32_t, respectively, in .
Type bool is a distinct type that has the same object representation, value representation, and alignment requirements as an implementation-defined unsigned integer type.
The values of type bool aretrue and false.
[Note 7:
There are no signed, unsigned,short, or long bool types or values.
â end note]
The types char, wchar_t,char8_t, char16_t, and char32_t are collectively called character types.
The character types, bool, the signed and unsigned integer types, and cv-qualified versions ([basic.type.qualifier]) thereof, are collectively termedintegral types.
A synonym for integral type is integer type.
[Note 8:
Enumerations ([dcl.enum]) are not integral; however, unscoped enumerations can be promoted to integral types as specified in [conv.prom].
â end note]
The three distinct typesfloat,double, andlong double can represent floating-point numbers.
The type double provides at least as much precision as float, and the type long double provides at least as much precision as double.
The set of values of the typefloat is a subset of the set of values of the typedouble; the set of values of the type double is a subset of the set of values of the type long double.
The typesfloat, double, and long double, and cv-qualified versions ([basic.type.qualifier]) thereof, are collectively termedstandard floating-point types.
An implementation may also provide additional types that represent floating-point values and define them (and cv-qualified versions thereof) to beextended floating-point types.
The standard and extended floating-point types are collectively termed floating-point types.
[Note 9:
Any additional implementation-specific types representing floating-point values that are not defined by the implementation to be extended floating-point types are not considered to be floating-point types, and this document imposes no requirements on them or their interactions with floating-point types.
â end note]
Except as specified in [basic.extended.fp], the object and value representations and accuracy of operations of floating-point types are implementation-defined.
The minimum range of representable values for a floating-point type is the most negative finite floating-point number representable in that type through the most positive finite floating-point number representable in that type.
In addition, if negative infinity is representable in a type, the range of that type is extended to all negative real numbers; likewise, if positive infinity is representable in a type, the range of that type is extended to all positive real numbers.
[Note 10:
Since negative and positive infinity are representable in ISO/IEC 60559 formats, all real numbers lie within the range of representable values of a floating-point type adhering to ISO/IEC 60559.
â end note]
Integral and floating-point types are collectively termed arithmetic types.
[Note 11:
Properties of the arithmetic types, such as their minimum and maximum representable value, can be queried using the facilities in the standard library headers,, and.
â end note]
A type cv void is an incomplete type that cannot be completed; such a type has an empty set of values.
It is used as the return type for functions that do not return a value.
An expression of type cv void shall be used only as
an expression statement ([stmt.expr]),
the expression in a return statement ([stmt.return]) for a function with the return type cv void,
an operand of a comma expression ([expr.comma]),
the second or third operand of ?: ([expr.cond]),
the operand of a typeid expression ([expr.typeid]),
the operand of a noexcept operator ([expr.unary.noexcept]),
the operand of a decltype specifier ([dcl.type.decltype]), or
the operand of an explicit conversion to typecv void ([expr.type.conv], [expr.static.cast], [expr.cast]).
The types denoted by cv std::nullptr_t are distinct types.
A prvalue of type std::nullptr_t is a null pointer constant ([conv.ptr]).
Such values participate in the pointer and the pointer-to-member conversions ([conv.ptr], [conv.mem]).
sizeof(std::nullptr_t) shall be equal to sizeof(void*).
A value of type std::meta::info is called a reflection.
There exists a unique null reflection; every other reflection is a representation of
a value of scalar type ([temp.param]),
an object with static storage duration ([basic.stc]),
a variable ([basic.pre]),
a structured binding ([dcl.struct.bind]),
a function ([dcl.fct]),
a function parameter,
an enumerator ([dcl.enum]),
an annotation ([dcl.attr.grammar]),
a type alias ([dcl.typedef]),
a type ([basic.types]),
a class member ([class.mem]),
an unnamed bit-field ([class.bit]),
a class template ([temp.pre]),
a function template,
a variable template,
an alias template ([temp.alias]),
a concept ([temp.concept]),
a namespace alias ([namespace.alias]),
a namespace ([basic.namespace.general]),
a direct base class relationship ([class.derived.general]), or
a data member description ([class.mem.general]).
A reflection is said to represent the corresponding construct.
[Note 12:
A reflection of a value can be produced by library functions such asstd::meta::constant_of and std::meta::reflect_constant.
â end note]
[Example 2: int arr[] = {1, 2, 3};auto [a1, a2, a3] = arr;void fn();enum Enum { A };using Alias = int;struct B {};struct S : B {int mem; int : 0;};template struct TCls {};template void TFn();template int TVar;template concept Concept = requires { true; };namespace NS {};namespace NSAlias = NS;
constexpr auto ctx = std::meta::access_context::current();
constexpr auto r1 = std::meta::reflect_constant(42); // represents int value of 42constexpr auto r2 = std::meta::reflect_object(arr[1]); // represents int objectconstexpr auto r3 = ^^arr; // represents a variableconstexpr auto r4 = ^^a3; // represents a structured bindingconstexpr auto r5 = ^^fn; // represents a functionconstexpr auto r6 = ^^Enum::A; // represents an enumeratorconstexpr auto r7 = ^^Alias; // represents a type aliasconstexpr auto r8 = ^^S; // represents a typeconstexpr auto r9 = ^^S::mem; // represents a class memberconstexpr auto r10 = std::meta::members_of(^^S, ctx)[1]; // represents an unnamed bit-fieldconstexpr auto r11 = ^^TCls; // represents a class templateconstexpr auto r12 = ^^TFn; // represents a function templateconstexpr auto r13 = ^^TVar; // represents a variable templateconstexpr auto r14 = ^^Concept; // represents a conceptconstexpr auto r15 = ^^NSAlias; // represents a namespace aliasconstexpr auto r16 = ^^NS; // represents a namespaceconstexpr auto r17 = std::meta::bases_of(^^S, ctx)[0]; // represents a direct base class relationshipconstexpr auto r18 = std::meta::data_member_spec(^^int, {.name="member"}); // represents a data member description â end example]
Recommended practice: Implementations should not represent other constructs specified in this document, such asusing-declarators, partial template specializations, attributes, placeholder types, statements, or expressions, as values of type std::meta::info.
[Note 13:
Future revisions of this document can specify semantics for reflections representing any such constructs.
â end note]
The types described in this subclause are called fundamental types.
[Note 14:
Even if the implementation defines two or more fundamental types to have the same value representation, they are nevertheless different types.
â end note]
This is also known as two's complement representation.