std::launder (3) - Linux Manuals
std::launder: std::launder
NAME
Synopsis
Defined in header <new>
template <class T> (since C++17)
constexpr T* launder(T* p) noexcept; (until C++20)
template <class T> (since C++20)
[[nodiscard]] constexpr T* launder(T* p) noexcept;
Obtains a pointer to the object located at the address represented by p.
Formally, given
* the pointer p represents the address A of a byte in memory
* an object X is located at the address A
* X is within its lifetime
* the type of X is the same as T, ignoring cv-qualifiers at every level
* every byte that would be reachable through the result is reachable through p (bytes are reachable through a pointer that points to an object Y if those bytes are within the storage of an object Z that is pointer-interconvertible with Y, or within the immediately enclosing array of which Z is an element)
Then std::launder(p) returns a value of type T* that points to the object X. Otherwise, the behavior is undefined.
The program is ill-formed if T is a function type or (possibly cv-qualified) void.
std::launder may be used in a core_constant_expression if the value of its argument may be used in a core constant expression.
Notes
std::launder has no effect on its argument. Its return value must be used to access the object. Thus, it's always an error to discard the return value.
Typical uses of std::launder include:
* Obtaining a pointer to an object created in the storage of an existing object of the same type, where pointers to the old object cannot be reused because the object has const or reference data members, or because either object is a base class subobject;
* Obtaining a pointer to an object created by placement new from a pointer to an object providing storage for that object.
The reachability restriction ensures that std::launder cannot be used to access bytes not accessible through the original pointer, thereby interfering with the compiler's escape analysis.
Example
// Run this code