C++ Logo

std-discussion

Advanced search

Re: [basic#life-6] unclear what happens if the storage is actually re-used

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Wed, 13 Dec 2023 08:11:07 +0100
On 13/12/2023 00.03, Mykola Garkusha via Std-Discussion wrote:
> Isn't [basic.life]/6 supposed to say that the behavior described in CWG1027 <https://cplusplus.github.io/CWG/issues/1027.html> is undefined, because technically lifetime of **int i = 3;** ended but its storage was not re-used yet (since re-use happens before the end of a lifetime and hence any pointer to its storage must be used in limited ways (similar to void*).
>
> After all, is there a clear happens before the relationship before re-use and end of a lifetime in placement new? Probably order between re-use and end of a lifetime is unspecified, as the standard says that the lifetime ends *when*(not before/after) storage is re-used)
>
> On Tue, Dec 12, 2023 at 10:13 PM Brian Bi via Std-Discussion <std-discussion_at_[hidden] <mailto:std-discussion_at_[hidden]>> wrote:
>
>
>
> On Tue, Dec 12, 2023 at 10:10 AM Neil Holmes <neilholmes91_at_[hidden] <mailto:neilholmes91_at_[hidden]>> wrote:
>
>
>
> On Tue, Dec 12, 2023 at 2:59 PM Brian Bi <bbi5291_at_[hidden] <mailto:bbi5291_at_[hidden]>> wrote:
>
>
>
> No, it doesn't mean that "any use" is undefined behavior. [basic.life]/6 implies that /some/ uses are well-defined, i.e. "using the pointer as if the pointer were of type void*" (which unfortunately it doesn't elaborate on), and gives a list of particular operations that /are/ UB.
>
>
>
>
> struct B {
> virtual void f();
> void mutate();
> virtual ~B();
> };
>
> struct D1 : B { void f(); };
> struct D2 : B { void f(); };
>
> void B::mutate() {
> new (this) D2; // reuses storage --- ends the lifetime of *this. Does it mean that if one uses placement new than first the storage is reused and *than* the object's lifetime ends?
> new (this) D2; // NOW STORAGE IS DEFINITELY RE-USED AND THE CONDITION in [basic#life-6] is not satisfied
> f(); // undefined behavior
> ... = *this*; // USING THIS IS UNDEFINED BEHAVIOR NOW?
> }
>
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden] <mailto:Std-Discussion_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion <https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion>
>
>
>
>
> Just to be clear I was looking what standard has to say about using pointers to the storage if the storage WAS re-used which means the pre-condition in [basic.life]/6 would not satisfy and the whole [basic.life]/6 should not be applied.
>
>
> Ah, I see. I misunderstood at first.
>
> This seems to be related to CWG1027 <https://cplusplus.github.io/CWG/issues/1027.html> which is not resolved because there was an idea that it would be resolved by CWG1116 <https://cplusplus.github.io/CWG/issues/1116.html>, whose proposed resolution was going to simply make certain replacements UB. Since CWG1116 was resolved differently from the proposed resolution, CWG1027 was not resolved and is still open.

I think we want to just strike "reused or" from basic.life p6+p7.
This way, it's clear that the original object still exists,
albeit outside of its lifetime, and the provisions apply.

Jens

Received on 2023-12-13 07:11:11