问题
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?
回答1:
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.
回答2:
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_ptrinstance fromr.
p22 Postconditions:*thisshall contain the old value ofr.rshall 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.
来源:https://stackoverflow.com/questions/8522740/is-the-state-of-any-standard-class-after-being-moved-specified