Does moving leave the object in a usable state?

2019-01-04 14:11发布

Say I have two vectors and I move one unto the other, v1 = std::move(v2); will v2 still be in a usable state after this?

3条回答
Root(大扎)
2楼-- · 2019-01-04 14:33

If you want to use v2 after the move, you will want to do something like:

v1 = std::move(v2);
v2.clear();

At this point, v1 will have the original contents of v2 and v2 will be in a well-defined empty state. This works on all of the STL containers (and strings, for that matter), and if you are implementing your own classes that support move-semantics, you'll probably want to do something similar.

If your particular implementation of the STL actually DOES leave the object in an empty state, then the second clear() will be essentially a no-op. In fact, if this is the case, it would be a legal optimization for a compiler to eliminate the clear() after the move.

查看更多
做自己的国王
3楼-- · 2019-01-04 14:44

No, it is left in an unspecified state.

Excerpt from open-std-org article -

.. move() gives its target the value of its argument, but is not obliged to preserve the value of its source. So, for a vector, move() could reasonably be expected to leave its argument as a zero-capacity vector to avoid having to copy all the elements. In other words, move is a potentially destructive read.

查看更多
Lonely孤独者°
4楼-- · 2019-01-04 14:51

From n3290, 17.6.5.15 Moved-from state of library types [lib.types.movedfrom]

  1. Objects of types defined in the C++ standard library may be moved from (12.8). Move operations may be explicitly specified or implicitly generated. Unless otherwise specified, such moved-from objects shall be placed in a valid but unspecified state.

Since the state is valid, this means you can safely operate on v2 (e.g. by assigning to it, which would put it back to a known state). Since it is unspecified however, it means you cannot for instance rely on any particular value for v2.empty() as long as it is in this state (but calling it won't crash the program).

Note that this axiom of move semantics ("Moved from objects are left in a valid but unspecified state") is something that all code should strive towards (most of the time), not just the Standard Library components. Much like the semantics of copy constructors should be making a copy, but are not enforced to.

查看更多
登录 后发表回答