C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Idea: moved_from<T>() for Efficient Moved-From State Construction

From: Bo Persson <bo_at_[hidden]>
Date: Thu, 24 Apr 2025 14:39:34 +0200
On 2025-04-24 at 09:32, Andrey Semashev via Std-Proposals wrote:
> On 24 Apr 2025 04:14, Jason McKesson via Std-Proposals wrote:
>>
>> Many `std::list` implementations need to have an allocated node to
>> function. So the default constructor creates one. But the move
>> constructor *also* allocates one for the moved-from object, since
>> moved-from objects have to still be valid (this is also why
>> `std::list`'s move constructor is not required to be noexcept). So
>> it's unclear when exactly this would be advantageous.
>
> A moved-from object only needs to be destructible and assignable-to. It
> doesn't need to be valid for any other operations. In your std::list
> example, the move assignment/constructor can leave the moved-from object
> without the node, if the std::list's destructor and assignment support
> that state.
>

A moved from standard container must support all operations that have no
preconditions, like empty(), begin(), and end(). *If* the implementation
uses the "extra" node for this, it will break.

Received on 2025-04-24 12:39:43