Date: Fri, 11 Jul 2025 20:41:56 +0300
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.
> 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.
Received on 2025-07-11 17:42:08