C++ Logo

std-discussion

Advanced search

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

From: Brian Bi <bbi5291_at_[hidden]>
Date: Tue, 12 Dec 2023 17:12:57 -0500
On Tue, Dec 12, 2023 at 10:10 AM Neil Holmes <neilholmes91_at_[hidden]> wrote:

>
>
> On Tue, Dec 12, 2023 at 2:59 PM Brian Bi <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]
>>> 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.


>
> So basically, if you understood me correctly, you are saying is that some
> uses of a pointer to the storage are well-defined and than it just does not
> tell what happens if the condition was not held - like in my example. It is
> than unspecified behavior if the object was re-used after its lifetime
> ended?
>
>
>> --
>> *Brian Bi*
>>
>

-- 
*Brian Bi*

Received on 2023-12-12 22:13:14