std::launder
| Defined in header <new> |  |  | 
|---|---|---|
| template <class T> constexpr T* launder(T* p) |  | (since C++17) | 
获取指向在同一类型的现有对象占用的存储中创建的对象的指针,即使该对象具有Const或引用成员。
正式的,给的。
- 指针p表示地址。A内存中的字节
- 对象X位于地址A
- X在它的范围内寿命
- 类型X是相同的T,忽略每个级别的cv-限定符。
- 通过结果可以到达的每个字节都可以通过p%28字节到达,如果这些字节位于对象%27s存储中,或者在对象的元素%29中,则可以通过指向对象的指针来访问这些字节。
然后std::launder(p)返回类型的值。T*指向对象的X...
如果程序格式不正确,则为T是函数类型还是%28可能是cv-限定%29?void...
std::launder可用于核心常数表达式如果它的参数的值可以用在核心常量表达式中。
例外
noexcept规格:
noexcept
注记
对于没有Const或引用成员的对象,std::launder没有必要;指针和引用可以是重用...
例
二次
#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在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

