C++ Logo

std-discussion

Advanced search

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

From: Mykola Garkusha <garkusha.mykola_at_[hidden]>
Date: Tue, 12 Dec 2023 23:03:05 +0000
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]> wrote:

>
>
> 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*
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2023-12-12 23:03:18