C++ Logo

std-discussion

Advanced search

Re: Standard guarantees on moved-from container

From: Hans Åberg <haberg-1_at_[hidden]>
Date: Mon, 1 Jul 2024 20:05:43 +0200
> On 1 Jul 2024, at 18:24, Daniel Krügler via Std-Discussion <std-discussion_at_[hidden]> wrote:
>
> Am Mo., 1. Juli 2024 um 17:33 Uhr schrieb Yongwei Wu via
> Std-Discussion <std-discussion_at_[hidden]>:
>>
>> On this page <URL:https://en.cppreference.com/w/cpp/container/vector/vector>,
>> I saw the wording "Move constructor. Constructs the container with the
>> contents of other using move semantics.... After the move, other is
>> guaranteed to be empty()."
>>
>> Does the standard really provide this empty() guarantee (for vector,
>> or any other containers)? I cannot find it. I only see the general
>> specification that a moved-from object is in a valid but unspecified
>> state.
>>
>> (If it does not provide this guarantee, should it?)
>
> The wording doesn't say that the post-condition is an empty state, but
> it has the requirement for all sequence containers (except for
> std::array) that the move operation has a constant complexity. This
> more or less implies an empty state for std::vector, since the
> move-target will typically just "steal" the guts from the move-source.
> Since you have the valid-but-unspecified guarantee you can call every
> function without preconditions (Such as size() or capacity()) and
> inspect the actual situation, if that would be relevant for you.

One way to implement it, at least in the past, is using swap. Has the standard been changed to prevent that?

Received on 2024-07-01 18:06:00