C++ Logo

std-discussion

Advanced search

Re: Does destruction clobber storage?

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Fri, 11 Jul 2025 22:09:36 +0200
On 11.07.25 20:33, Hans Åberg via Std-Discussion wrote:
>
>> On 11 Jul 2025, at 19:41, Ell <ell_se_at_[hidden]> wrote:
>>
>> On 7/11/25 10:38, Hans Åberg wrote:
>>> The standard just says that the moved-from object should be left in a valid but otherwise unspecified state, and there are different ways to implement this. One is using a swap, but the destruction order gets out of sync, and it may not be as fast as first destroying the moved-to object, move, and then set a suitable value to the moved-from object, such as nullptr in the case of pointers. It looks, in the case of std::unique_ptr, as though GCC is using the swap method and Clang the destroy and move method.
>>
>> This isn't about the specifics of the moved-from state (which is always
>> nullptr for unique_ptr), but the fact that clang bothers to zero-out the
>> region of the vector's storage where the popped unique_ptr used to live,
>> even though it optimizes away the null check in the destructor.
>> Supposedly, it does that because you (meaning the stdlib author) could
>> observe the assignment by inspecting the vector's storage (at least,
>> clang seems to let you do that with consistent results). So the question
>> was if that's actually required behavior.
>
> GCC implements std::vector with three pointers: to the base, one past respectively the allocation and storage. My interpretation is that when the storage is reduced, the storage pointer can be adjusted, but nothing else is required. If something else is allowed, that would be expressed by the complexity requirement, which is here constant time, but it is only one element that is popped, so zeroing out would seem to be allowed. But it can't reallocate, as that would not be constant time.

We're discussing semantics of the core language here;
the standard library is totally irrelevant for this
discussion. (Although the examples presented originate
from the standard library.)

Jens

Received on 2025-07-11 20:09:47