What can I do with a moved-from object?

后端 未结 2 2354
轮回少年
轮回少年 2020-11-22 05:39

Does the standard define precisely what I can do with an object once it has been moved from? I used to think that all you can do with a moved-from object is do destruct it,

2条回答
  •  野的像风
    2020-11-22 06:05

    17.6.5.15 [lib.types.movedfrom]

    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.

    When an object is in an unspecified state, you can perform any operation on the object which has no preconditions. If there is an operation with preconditions you wish to perform, you can not directly perform that operation because you do not know if the unspecified-state of the object satisfies the preconditions.

    Examples of operations that generally do not have preconditions:

    • destruction
    • assignment
    • const observers such as get, empty, size

    Examples of operations that generally do have preconditions:

    • dereference
    • pop_back

    This answer now appears in video format here: http://www.youtube.com/watch?v=vLinb2fgkHk&t=47m10s

提交回复
热议问题