C++ Logo

std-discussion

Advanced search

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

From: Neil Holmes <neilholmes91_at_[hidden]>
Date: Tue, 12 Dec 2023 15:10:00 +0000
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.

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*
>

Received on 2023-12-12 15:10:13