Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Are moved from objects save to use

36 views
Skip to first unread message

Volker Wippel

unread,
Apr 14, 2015, 5:50:13 AM4/14/15
to
Hi

In C++11:
17.6.5.15 ...Unless otherwise specified, such moved-from objects
shall be placed in a valid but unspecified state.

std::vector<int> x = f();
auto const y = std::move(x);
x.push_back(1);... //use x like a "fresh" empty vector?
assert(1 == x.size());

For vectors or other STL containers and other classes, is this
"unspecified state" guarantied to allow save operations except
deletion? All or just move into, assignment?

Thanks, Volker

Jorgen Grahn

unread,
Apr 14, 2015, 6:42:34 AM4/14/15
to
On Tue, 2015-04-14, Volker Wippel wrote:
> Hi
>
> In C++11:
> 17.6.5.15 ...Unless otherwise specified, such moved-from objects
> shall be placed in a valid but unspecified state.
>
> std::vector<int> x = f();
> auto const y = std::move(x);
> x.push_back(1);... //use x like a "fresh" empty vector?
> assert(1 == x.size());

The text you quote above contradicts your assertion.
x may just as well contain { 1, 2, 3 } at that point.

> For vectors or other STL containers and other classes, is this
> "unspecified state" guarantied to allow save operations except
> deletion? All or just move into, assignment?

Please clarify those questions; I can barely guess what you mean.
Do you mean "safe" rather than "save" for example?

I assume "valid but unspecified" means exactly that -- x is in one of
the infinite number of valid states cor a vector<int>, but you don't
know which one.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Victor Bazarov

unread,
Apr 14, 2015, 7:53:30 AM4/14/15
to
On 4/14/2015 5:49 AM, Volker Wippel wrote:
> In C++11:
> 17.6.5.15 ...Unless otherwise specified, such moved-from objects
> shall be placed in a valid but unspecified state.
>
> std::vector<int> x = f();
> auto const y = std::move(x);
> x.push_back(1);... //use x like a "fresh" empty vector?
> assert(1 == x.size());
>
> For vectors or other STL containers and other classes, is this
> "unspecified state" guarantied to allow save operations except
> deletion?

See Jorgen's reply, and also my question:
Why not deletion? What prevents you from doing
x.clear();
to ensure it's empty before you start inserting into it?

> All or just move into, assignment?

Everything is OK as long as you don't assume it contains any useful
data. It might or it might not, that's the whole idea of "unspecified
state". Feel free to query its size as well. And if it does have some
data (x.empty() == false), you can extract those too, not sure of what
use they are going to be.

V
--
I do not respond to top-posted replies, please don't ask

Öö Tiib

unread,
Apr 14, 2015, 12:17:01 PM4/14/15
to
On Tuesday, 14 April 2015 12:50:13 UTC+3, Volker Wippel wrote:
> Hi
>
> In C++11:
> 17.6.5.15 ...Unless otherwise specified, such moved-from objects
> shall be placed in a valid but unspecified state.

Others already answered your puzzle, I just add few
other related notes:

* Standard "specifies otherwise" sometimes; for example
moved from 'std::unique_ptr' is guaranteed to be in empty state.
* 'std::move' has misleading name since calling it does not
automatically result with its argument being moved from.
* Doing other things besides copying or moving in
user-defined copy or move constructor can work like
boobytrap because C++ program may elide moves and
copies in number of situations.
* Move in Visual C++ 2013 is not conforming so if it is
among target platforms then standard is not best source
of knowledge.
0 new messages