[basic.compound] (original) (raw)
6 Basics [basic]
6.8 Types [basic.types]
6.8.4 Compound types [basic.compound]
Compound types can be constructed in the following ways:
- functions, which have parameters of given types and returnvoid or a result of a given type, [dcl.fct];
- pointers to cv void or objects or functions (including static members of classes) of a given type, [dcl.ptr];
- references to objects or functions of a given type, [dcl.ref].
There are two types of references:- lvalue reference
- rvalue reference
- unions, which are classes capable of containing objects of different types at different times, [class.union];
- enumerations, which comprise a set of named constant values, [dcl.enum];
- pointers to non-static class members,31which identify members of a given type within objects of a given class, [dcl.mptr].
Pointers to data members and pointers to member functions are collectively called pointer-to-member types.
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 pointer to an object or function (the pointer is said to point to the object or function), or
- a pointer past the end of an object ([expr.add]), or
- the null pointer value for that type, or
- an invalid pointer value.
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
- they are the same object, or
- one is a union object and the other is a non-static data member of that object ([class.union]), or
- one is a standard-layout class object and the other is the first non-static data member of that object or any base class subobject of that object ([class.mem]), or
- there exists an object c such that_a_ and c are pointer-interconvertible, and_c_ and b are pointer-interconvertible.
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”.