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?
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.
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.
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.
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.
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 toT*
.
p21 Effects: Move-constructs ashared_ptr
instance fromr
.
p22 Postconditions:*this
shall contain the old value ofr
.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With