C++ Logo

std-discussion

Advanced search

Re: Standard guarantees on moved-from container

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Mon, 1 Jul 2024 18:34:25 +0000
It could be implemented by using swap, but it could also be implemented by not using swap, that's the thing.

-----Original Message-----
From: Std-Discussion <std-discussion-bounces_at_lists.isocpp.org> On Behalf Of Hans Åberg via Std-Discussion
Sent: Monday, July 1, 2024 20:06
To: Std-Discussion <std-discussion_at_lists.isocpp.org>
Cc: Hans Åberg <haberg-1_at_telia.com>
Subject: Re: [std-discussion] Standard guarantees on moved-from container


> On 1 Jul 2024, at 18:24, Daniel Krügler via Std-Discussion <std-discussion_at_lists.isocpp.org> wrote:
>
> Am Mo., 1. Juli 2024 um 17:33 Uhr schrieb Yongwei Wu via
> Std-Discussion <std-discussion_at_lists.isocpp.org>:
>>
>> 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?


--
Std-Discussion mailing list
Std-Discussion_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion

Received on 2024-07-01 18:34:32