Defined in header <new> | ||
---|---|---|
template <class T> constexpr T* launder(T* p) | (since C++17) |
Obtains a pointer to an object created in storage occupied by an existing object of the same type, even if it has const or reference members.
Formally, given.
p
represents the address A
of a byte in memory X
is located at the address A
X
is within its lifetime X
is the same as T
, ignoring cv-qualifiers at every level Then std::launder(p)
returns a value of type T*
that points to the object X
.
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.
noexcept
specification: noexcept
For objects that do not have const or reference members, std::launder
is not necessary; pointers and references can be reused.
#include <new> struct X { const int n; // note: X has a const member int m; }; int main() { X *p = new X{3}; const int a = p->n; new (p) X{5}; // p does not point to new object because X::n is const const int b = p->n; // undefined behavior const int x = p->m; // undefined behavior (even though m is non-const, p can't be used) const int c = std::launder(p)->n; // OK, std::launder(p) points to new object }
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/launder