Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the state of any standard class after being moved specified?

If I move shared_ptr 'a' into shared_ptr 'b' is 'a' guaranteed to be null?

Is the state of any standard class after being moved specified?

like image 989
David Avatar asked Dec 15 '11 15:12

David


People also ask

What does std move mean?

std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular, std::move produces an xvalue expression that identifies its argument t . It is exactly equivalent to a static_cast to an rvalue reference type.

How does std move work?

std::move is a cast. It takes any value as argument and returns that same value in the xvalue category. And a value of type T and category xvalue is denoted thus: T&& . The move operation itself is performed by one of the constructors of the object to which it moves.

Is destructor called after move?

uninitialized_move() initializes new T objects into the new memory area by moving them from the old memory area. Then it calls the destructor on the original T object, the moved-from object.

Where is std :: move defined?

In C++11, std::move is a standard library function that casts (using static_cast) its argument into an r-value reference, so that move semantics can be invoked. Thus, we can use std::move to cast an l-value into a type that will prefer being moved over being copied. std::move is defined in the utility header.


2 Answers

If specified, it's under their constructor and (if assignable) assignment operator subclause. For shared_ptr we have:

§20.7.2.2.1 [util.smartptr.shared.const]

shared_ptr(shared_ptr&& r) noexcept;
template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;

p20 Remark: The second constructor shall not participate in overload resolution unless Y* is convertible to T*.
p21 Effects: Move-constructs a shared_ptr instance from r.
p22 Postconditions: *this shall contain the old value of r. r shall be empty. r.get() == 0.

The assignment operators of shared_ptr are basically describes by copy-and-swap with a temporary constructed from the (moved if rvalue) argument:

§20.7.2.2.3 [util.smartptr.shared.assign]

shared_ptr& operator=(shared_ptr&& r) noexcept;
template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r) noexcept;

p4 Effects: Equivalent to shared_ptr(std::move(r)).swap(*this).
p5 Returns: *this.

If not specified, what @AProgrammer said applies.

like image 79
Xeo Avatar answered Sep 30 '22 22:09

Xeo


In general 17.6.5.15/1 applies:

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.

Thus you can call any functions which requires no precondition.

If specified, what @Xeo said applies.

like image 30
AProgrammer Avatar answered Sep 30 '22 22:09

AProgrammer