Home > database >  Which version of C standard allows reuse of storage previously occupied by an object of a class th
Which version of C standard allows reuse of storage previously occupied by an object of a class th

Time:05-16

This answer cites some unknown revision of C standard draft:

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

This means that the following code is invalid if class A has const or reference members:

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

The current revision of [basic.life]p8 doesn't have this requirement:

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.

This make the code above valid.

But both citations are from the draft. So I don't know starting from witch version of the standard I can use the code above for the class objects that have const or reference members. The answer date is May 7, 2018. So I guess it can be only C 20?

CodePudding user response:

In terms of the "Major" Standard releases, the clause about the const qualification (which you have emphasised in the excerpt you cite in your question) was present in the final draft for the C 17 Standard (N4659) but not present in that for the C 20 Standard (N4861).

So, from that it would appear that conformance to C 20 (or later) is required to reuse storage previously occupied by a const- or reference-containing class object (in this context).

  • Related