17 Language support library [support] (original) (raw)

17.6 Dynamic memory management [support.dynamic]

17.6.5 Pointer optimization barrier [ptr.launder]

template<class T> [[nodiscard]] constexpr T* launder(T* p) noexcept;

Mandates: !is_­function_­v<T> && !is_­void_­v<T> is true.

Preconditions: p represents the address A of a byte in memory.

An object X that is within its lifetimeand whose type is similar to Tis located at the address A.

All bytes of storage that would be reachable through the result are reachable through p (see below).

Returns: A value of type T* that points to X.

Remarks: An invocation of this function may be used in a core constant expression whenever the value of its argument may be used in a core constant expression.

A byte of storage b is reachable through a pointer value that points to an object Y_if there is an object Z, pointer-interconvertible with Y, such that b is within the storage occupied by_Z, or the immediately-enclosing array object if Z is an array element.

[Note 1:

If a new object is created in storage occupied by an existing object of the same type, a pointer to the original object can be used to refer to the new object unless its complete object is a const object or it is a base class subobject; in the latter cases, this function can be used to obtain a usable pointer to the new object.

— _end note_]

[Example 1: struct X { int n; };const X *p = new const X{3};const int a = p->n;new (const_cast<X*>(p)) const X{5}; const int b = p->n; const int c = std::launder(p)->n; — _end example_]