[basic.types] (original) (raw)

6 Basics [basic]

6.8.1 General [basic.types.general]

[Note 1:

[basic.types] and the subclauses thereof impose requirements on implementations regarding the representation of types.

There are two kinds of types: fundamental types and compound types.

— _end note_]

For any object (other than a potentially-overlapping subobject) of trivially copyable typeT, whether or not the object holds a valid value of typeT, the underlying bytes ([intro.memory]) making up the object can be copied into an array ofchar,unsigned char, orstd​::​byte ([cstddef.syn]).26

If the content of that array is copied back into the object, the object shall subsequently hold its original value.

[Example 1: constexpr std::size_t N = sizeof(T);char buf[N]; T obj; std::memcpy(buf, &obj, N); std::memcpy(&obj, buf, N); — _end example_]

For two distinct objects obj1 and obj2of trivially copyable type T, where neither obj1 nor obj2 is a potentially-overlapping subobject, if the underlying bytes ([intro.memory]) making upobj1 are copied into obj2,27 obj2 shall subsequently hold the same value asobj1.

[Example 2: T* t1p; T* t2p; std::memcpy(t1p, t2p, sizeof(T)); — _end example_]

The object representationof a complete object type T is the sequence of N unsigned char objects taken up by a non-bit-field complete object of type T, where N equalssizeof(T).

The value representationof a type T is the set of bits in the object representation of Tthat participate in representing a value of type T.

The object and value representation of a non-bit-field complete object of type T are the bytes and bits, respectively, of the object corresponding to the object and value representation of its type.

The object representation of a bit-field object is the sequence of N bits taken up by the object, where N is the width of the bit-field ([class.bit]).

The value representation of a bit-field object is the set of bits in the object representation that participate in representing its value.

Bits in the object representation of a type or object that are not part of the value representation are padding bits.

For trivially copyable types, the value representation is a set of bits in the object representation that determines avalue, which is one discrete element of animplementation-defined set of values.28

A class that has been declared but not defined, an enumeration type in certain contexts ([dcl.enum]), or an array of unknown bound or of incomplete element type, is anincompletely-defined object type.29

Incompletely-defined object types and cv void areincomplete types ([basic.fundamental]).

[Note 2:

Objects cannot be defined to have an incomplete type ([basic.def]).

— _end note_]

A class type (such as “class X”) can be incomplete at one point in a translation unit and complete later on; the type “class X” is the same type at both points.

The declared type of an array object can be an array of incomplete class type and therefore incomplete; if the class type is completed later on in the translation unit, the array type becomes complete; the array type at those two points is the same type.

The declared type of an array object can be an array of unknown bound and therefore be incomplete at one point in a translation unit and complete later on; the array types at those two points (“array of unknown bound of T” and “array ofN T”) are different types.

[Note 3:

The type of a pointer or reference to array of unknown bound permanently points to or refers to an incomplete type.

An array of unknown bound named by a typedef declaration permanently refers to an incomplete type.

In either case, the array type cannot be completed.

— _end note_]

[Example 3: class X; extern X* xp; extern int arr[]; typedef int UNKA[]; UNKA* arrp; UNKA** arrpp;void foo() { xp++; arrp++; arrpp++; } struct X { int i; }; int arr[10]; X x;void bar() { xp = &x; arrp = &arr; xp++; arrp++; } — _end example_]

[Note 4:

The rules for declarations and expressions describe in which contexts incomplete types are prohibited.

— _end note_]

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not cv void.

Arithmetic types ([basic.fundamental]), enumeration types, pointer types, pointer-to-member types ([basic.compound]),std​::​nullptr_t, andcv-qualified versions of these types are collectively calledscalar types.

Scalar types, trivially copyable class types ([class.prop]), arrays of such types, and cv-qualified versions of these types are collectively called trivially copyable types.

Scalar types, trivially relocatable class types ([class.prop]), arrays of such types, and cv-qualified versions of these types are collectively called trivially relocatable types.

Cv-unqualified scalar types, replaceable class types ([class.prop]), and arrays of such types are collectively calledreplaceable types.

Scalar types, standard-layout class types ([class.prop]), arrays of such types, and cv-qualified versions of these types are collectively called standard-layout types.

Scalar types, implicit-lifetime class types ([class.prop]), array types, and cv-qualified versions of these types are collectively called implicit-lifetime types.

A type is a literal type if it is:

[Note 5:

A literal type is one for which it might be possible to create an object within a constant expression.

It is not a guarantee that it is possible to create such an object, nor is it a guarantee that any object of that type will be usable in a constant expression.

— _end note_]

6.8.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 to (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 (inclusive); arithmetic for the unsigned type is performed modulo .

[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 has the same value of corresponding bits in its value representation.30

[Example 1:

The value of a signed integer type has the same representation as the largest value of the corresponding unsigned type.

— _end example_]

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:2018 6.2.6.2.

— _end note_]

[Note 4:

The signed and unsigned integer types satisfy the constraints given in ISO/IEC 9899:2018 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 , where each coefficient 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 charare collectively calledordinary character types.

The ordinary character types and char8_tare 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_tare 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 doublecan 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 voidis 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

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*).

The types described in this subclause are called fundamental types.

[Note 12:

Even if the implementation defines two or more fundamental types to have the same value representation, they are nevertheless different types.

— _end note_]

6.8.3 Optional extended floating-point types [basic.extended.fp]

If the implementation supports an extended floating-point type ([basic.fundamental]) whose properties are specified by the ISO/IEC 60559 floating-point interchange format binary16, then the typedef-name std​::​float16_tis declared in the header and names such a type, the macro __STDCPP_FLOAT16_T__ is defined ([cpp.predefined]), and the floating-point literal suffixes f16 and F16are supported ([lex.fcon]).

If the implementation supports an extended floating-point type whose properties are specified by the ISO/IEC 60559 floating-point interchange format binary32, then the typedef-name std​::​float32_tis declared in the header and names such a type, the macro __STDCPP_FLOAT32_T__ is defined, and the floating-point literal suffixes f32 and F32 are supported.

If the implementation supports an extended floating-point type whose properties are specified by the ISO/IEC 60559 floating-point interchange format binary64, then the typedef-name std​::​float64_tis declared in the header and names such a type, the macro __STDCPP_FLOAT64_T__ is defined, and the floating-point literal suffixes f64 and F64 are supported.

If the implementation supports an extended floating-point type whose properties are specified by the ISO/IEC 60559 floating-point interchange format binary128, then the typedef-name std​::​float128_tis declared in the header and names such a type, the macro __STDCPP_FLOAT128_T__ is defined, and the floating-point literal suffixes f128 and F128 are supported.

If the implementation supports an extended floating-point type with the properties, as specified by ISO/IEC 60559, of radix (b) of 2, storage width in bits (k) of 16, precision in bits (p) of 8, maximum exponent (emax) of 127, and exponent field width in bits (w) of 8, then the typedef-name std​::​bfloat16_tis declared in the header and names such a type, the macro __STDCPP_BFLOAT16_T__ is defined, and the floating-point literal suffixes bf16 and BF16 are supported.

[Note 1:

A summary of the parameters for each type is given in Table 15.

The precision p includes the implicit 1 bit at the beginning of the significand, so the storage used for the significand is bits.

ISO/IEC 60559 does not assign a name for a type having the parameters specified for std​::​bfloat16_t.

— _end note_]

Table 15 — Properties of named extended floating-point types [tab:basic.extended.fp]

🔗Parameter float16_t float32_t float64_t float128_t bfloat16_t
🔗ISO/IEC 60559 name binary16 binary32 binary64 binary128
🔗k, storage width in bits 16 32 64 128 16
🔗p, precision in bits 11 24 53 113 8
🔗emax, maximum exponent 15 127 1023 16383 127
🔗w, exponent field width in bits 5 8 11 15 8

Recommended practice: Any names that the implementation provides for the extended floating-point types described in this subsection that are in addition to the names declared in the header should be chosen to increase compatibility and interoperability with the interchange types_Float16, _Float32, _Float64, and _Float128defined in ISO/IEC TS 18661-3 and with future versions of ISO/IEC 9899.

6.8.4 Compound types [basic.compound]

Compound types can be constructed in the following ways:

These methods of constructing types can be applied recursively; restrictions are mentioned in [dcl.meaning].

Constructing a type such that the number of bytes in its object representation exceeds the maximum value representable in the type std​::​size_t ([support.types]) is ill-formed.

The type of a pointer to cv void or a pointer to an object type is called an object pointer type.

[Note 1:

A pointer to voiddoes not have a pointer-to-object type, however, because void is not an object type.

— _end note_]

The type of a pointer that can designate a function is called a function pointer type.

A pointer to an object of type T is referred to as a “pointer toT”.

[Example 1:

A pointer to an object of type int is referred to as “pointer to int” and a pointer to an object of class X is called a “pointer to X”.

— _end example_]

Except for pointers to static members, text referring to “pointers” does not apply to pointers to members.

Pointers to incomplete types are allowed although there are restrictions on what can be done with them ([basic.types.general]).

Every value of pointer type is one of the following:

A value of a pointer type that is a pointer to or past the end of an objectrepresents the address of the first byte in memory ([intro.memory]) occupied by the object32or the first byte in memory after the end of the storage occupied by the object, respectively.

[Note 2:

A pointer past the end of an object ([expr.add]) is not considered to point to an unrelated object of the object's type, even if the unrelated object is located at that address.

— _end note_]

For purposes of pointer arithmetic ([expr.add]) and comparison ([expr.rel], [expr.eq]), a pointer past the end of the last element of an array x of n elements is considered to be equivalent to a pointer to a hypothetical array element n of x, and an object of type T that is not an array element is considered to belong to an array with one element of type T.

The value representation of pointer types is implementation-defined.

Pointers to layout-compatible types shall have the same value representation and alignment requirements ([basic.align]).

[Note 3:

Pointers to over-aligned types have no special representation, but their range of valid values is restricted by the extended alignment requirement.

— _end note_]

A pointer value P isvalid in the context of an evaluation Eif P is a pointer to function or a null pointer value, or if it is a pointer to or past the end of an object O andE happens before the end of the duration of the region of storage for O.

If a pointer value P is used in an evaluation E andP is not valid in the context of E, then the behavior is undefined if E is an indirection ([expr.unary.op]) or an invocation of a deallocation function ([basic.stc.dynamic.deallocation]), and implementation-defined otherwise.33

[Note 4:

P can be valid in the context of E even if it points to a type unrelated to that of O or if O is not within its lifetime, although further restrictions apply to such pointer values ([basic.life], [basic.lval], [expr.add]).

— _end note_]

Two objects a and b are pointer-interconvertible if

If two objects are pointer-interconvertible, then they have the same address, and it is possible to obtain a pointer to one from a pointer to the other via a reinterpret_cast ([expr.reinterpret.cast]).

[Note 5:

An array object and its first element are not pointer-interconvertible, even though they have the same address.

— _end note_]

A byte of storage _b_is reachable througha pointer value that points to an object x_if there is an object y, pointer-interconvertible with x, such that b is within the storage occupied by_y, or the immediately-enclosing array object if y is an array element.

A pointer to cv voidcan be used to point to objects of unknown type.

Such a pointer shall be able to hold any object pointer.

An object of type “pointer to cv void” shall have the same representation and alignment requirements as an object of type “pointer to cv char”.

6.8.5 CV-qualifiers [basic.type.qualifier]

Each type other than a function or reference type is part of a group of four distinct, but related, types: a cv-unqualified version, a const-qualified version, a volatile-qualified version, and a const-volatile-qualified version.

The types in each such group shall have the same representation and alignment requirements ([basic.align]).34

A function or reference type is always cv-unqualified.

Except for array types, a compound type ([basic.compound]) is not cv-qualified by the cv-qualifiers (if any) of the types from which it is compounded.

An array type whose elements are cv-qualified is also considered to have the same cv-qualifications as its elements.

[Note 2:

Cv-qualifiers applied to an array type attach to the underlying element type, so the notation “cv T”, where T is an array type, refers to an array whose elements are so-qualified ([dcl.array]).

— _end note_]

[Example 1: typedef char CA[5];typedef const char CC; CC arr1[5] = { 0 };const CA arr2 = { 0 };

The type of both arr1 and arr2 is “array of 5const char”, and the array type is considered to be const-qualified.

— _end example_]

There is a partial ordering on cv-qualifiers, so that a type can be said to be more cv-qualified than another.

Table 16 shows the relations that constitute this ordering.

Table 16 — Relations on const and volatile [tab:basic.type.qualifier.rel]

🔗no cv-qualifier < const
🔗no cv-qualifier < volatile
🔗no cv-qualifier < const volatile
🔗const < const volatile
🔗volatile < const volatile

In this document, the notation cv (or_cv1_, cv2, etc.)

, used in the description of types, represents an arbitrary set of cv-qualifiers, i.e., one of {const}, {volatile}, {const,volatile}, or the empty set.

For a type cv T, the top-level cv-qualifiersof that type are those denoted by cv.

[Example 2:

The type corresponding to the type-id const int&has no top-level cv-qualifiers.

The type corresponding to the type-id volatile int * consthas the top-level cv-qualifier const.

For a class type C, the type corresponding to the type-id void (C​::​* volatile)(int) consthas the top-level cv-qualifier volatile.

— _end example_]

6.8.6 Conversion ranks [conv.rank]

Every integer type has an integer conversion rank defined as follows:

[Note 1:

The integer conversion rank is used in the definition of the integral promotions ([conv.prom]) and the usual arithmetic conversions ([expr.arith.conv]).

— _end note_]

Every floating-point type has a floating-point conversion rankdefined as follows:

[Note 3:

The conversion ranks of floating-point types T1 and T2are unordered if the set of values of T1 is neither a subset nor a superset of the set of values of T2.

This can happen when one type has both a larger range and a lower precision than the other.

— _end note_]

Floating-point types that have equal floating-point conversion ranks are ordered by floating-point conversion subrank.

The subrank forms a total order among types with equal ranks.

The typesstd​::​float16_t,std​::​float32_t,std​::​float64_t, andstd​::​float128_t ([stdfloat.syn]) have a greater conversion subrank than any standard floating-point type with equal conversion rank.

Otherwise, the conversion subrank order isimplementation-defined.

[Note 4:

The floating-point conversion rank and subrank are used in the definition of the usual arithmetic conversions ([expr.arith.conv]).

— _end note_]