[basic.types.general] (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]).27

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,28 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.29

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.30

[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, trivial class types ([class.prop]), arrays of such types, and cv-qualified versions of these types are collectively calledtrivial 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_]