C++ Logo

std-discussion

Advanced search

Re: Standard guarantees on moved-from container

From: Daniel Krügler <daniel.kruegler_at_[hidden]>
Date: Mon, 1 Jul 2024 18:24:47 +0200
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.

Thanks,

- Daniel

Received on 2024-07-01 16:25:01