移动后使用 C++:有效用例?
C++ use after move: Valid use-case?
一个简单的问题,在移动后使用拥有唯一指针的对象,并在未移动的情况下继续使用唯一指针是否合法?
标准保证移出的 unique_ptr
比较等于 nullptr
。 N4659 [unique.ptr]/4:
Additionally, u
can, upon request, transfer ownership to another unique pointer u2
. Upon completion of such a transfer, the following postconditions hold:
- (4.1)
u2.p
is equal to the pre-transfer u.p
,
- (4.2)
u.p
is equal to nullptr
, and
- (4.3) if the pre-transfer
u.d
maintained state, such state has been transferred to u2.d
.
这些保证还意味着可以安全地从已经搬离的地方搬走。
一个简单的问题,在移动后使用拥有唯一指针的对象,并在未移动的情况下继续使用唯一指针是否合法?
标准保证移出的 unique_ptr
比较等于 nullptr
。 N4659 [unique.ptr]/4:
Additionally,
u
can, upon request, transfer ownership to another unique pointeru2
. Upon completion of such a transfer, the following postconditions hold:
- (4.1)
u2.p
is equal to the pre-transferu.p
,- (4.2)
u.p
is equal tonullptr
, and- (4.3) if the pre-transfer
u.d
maintained state, such state has been transferred tou2.d
.
这些保证还意味着可以安全地从已经搬离的地方搬走。