c++ - 哪个版本的 C++ 标准允许重用先前由具有 const 或引用成员的类的对象占用的存储空间?

标签 c++ language-lawyer

This answer引用了一些未知的 C++ 标准草案修订版:

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if:

  • the storage for the new object exactly overlays the storage location which the original object occupied, and

  • the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and

  • the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and

  • neither the original object nor the new object is a potentially-overlapping subobject ([intro.object]).

这意味着如果类 A 具有 const 或引用成员,则以下代码无效:

A a;
a.~A();
new (&a) A;

[basic.life]p8 的当前版本没有这个要求:

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if the original object is transparently replaceable (see below) by the new object. An object o1 is transparently replaceable by an object o2 if:

  • the storage that o2 occupies exactly overlays the storage that o1 occupied, and
  • o1 and o2 are of the same type (ignoring the top-level cv-qualifiers), and
  • o1 is not a complete const object, and
  • neither o1 nor o2 is a potentially-overlapping subobject ([intro.object]), and
  • either o1 and o2 are both complete objects, or o1 and o2 are direct subobjects of objects p1 and p2, respectively, and p1 is transparently replaceable by p2.

这使得上面的代码有效。

但这两个引文都来自草稿。所以我不知道从标准的女巫版本开始我可以将上面的代码用于具有 const 或引用成员的类对象。答案日期是2018年5月7日。所以我猜它只能是C++20?

最佳答案

就“主要”标准版本而言,关于 const 资格的条款(您在问题中引用的摘录中强调了这一点)出现在 final draft for the C++17 Standard 中。 (N4659) 但出现在 that for the C++20 Standard 中(N4861).

因此,从中可以看出,符合 C++20(或更高版本)需要重用以前由 const 或包含引用占用的存储空间类对象(在此上下文中)。

关于c++ - 哪个版本的 C++ 标准允许重用先前由具有 const 或引用成员的类的对象占用的存储空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72250710/

相关文章:

c++ - VS 2013 无法根据模板参数专门化具有通用引用和返回类型的函数模板

c++ - 是否有可能有一个零成本的 assert() 使得在调试和发布构建之间不必修改代码?

c++ - C(和 C++)中 char 的对齐是否保证为 1?

c++ - "Unpack"一个用可变参数模板调用函数的数组

C++ 元编程 - 编译时搜索树

c++ - 如何隐式调用 C 风格的清洁函数?

java - 非捕获 lambda 似乎仍然捕获了封闭的实例

c++ - [conv.integral]/3 在最新的 C++ 草案中

c++ - 根据标准,这种没有破坏的交换实现是否有效?

c++ - 为什么转换可以涉及两个用户定义的转换函数/构造函数?