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

我与影子孤独终老i 提交于 2019-12-08 17:08:02

问题


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 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.



来源:https://stackoverflow.com/questions/8522740/is-the-state-of-any-standard-class-after-being-moved-specified

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!