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

Which version of C++ standard allows reuse of storage previously occupied by an object of a class that has const or reference members?

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]).

这意味着如果 class 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 或引用成员的 class 对象。回答日期是2018年5月7日。所以我猜它只能是C++20?

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

因此,从中可以看出,符合 C++20(或更高版本)需要重用以前由 const- 或 reference-containing class 对象(在此上下文中)。