在 move 构造函数中用作 arg 后,哪些 std 类型保证为 empty/null
Which std types are guaranteed to be empty/null after being used as arg in move constructor
我知道 shared_ptr
, unique_ptr
, weak_ptr
在相同类型的构造函数中用作 RVR 参数后保证为空,但我不知道标准是否指定了这一点除了我提到的那些之外,还有一些其他 std::
类型。
请注意,我知道移动后的元素处于有效但 未指定 状态,我在这里对指定了哪些类型状态感兴趣。
使 moved-from 对象处于 "empty" 状态的类型是智能指针、锁 ([thread.lock.unique.cons]/21, [thread.lock.shared.cons]/21), file streams ([filebuf.cons]/(4.2)), futures ([futures.unique_future]/(8.2), [futures.shared_future]/10), promises ([futures.promise]/6), packaged tasks ([futures.task]/7), threads ([thread.thread.constr]/10)、...
相比之下,留下 moved-from 个未指定值的对象的模板是 function
([func.wrap.func.con]/6), basic_regex
([re.regex.construct]/13), basic_string
([string.cons]/2),容器…
经验法则
根据经验:move-only 类型或具有共享引用语义的类型将它们的 moved-from 对象留在空状态。 所有其他类型保留未指定的值。
Move-only 类型
Move-only 类型(使 moved-from 对象处于空状态)是 std::unique_lock
、std::thread
、std::promise
、std::packaged_task
、 std::future
、basic_filebuf
、std::basic_ifstream
、std::basic_ofstream
、std::basic_fstream
、std::shared_lock
和 std::unique_ptr
。
共享引用语义
具有共享引用语义的类型是 std::shared_future
,当然还有 std::shared_ptr
和 std::weak_ptr
。它们的 moved-from 对象也处于空状态。
一个值得注意的例外
当我浏览标准库时,我发现 std::stringstream
及其 input-only 和 output-only 兄弟姐妹(std::istringstream
和 std::ostringstream
)是一个值得注意的例外。这些 类 是 move-only,但没有关于 move-construction 上的 moved-from 对象的信息。因此,valid-but-unspecified-rule 适用。如您所见,这只是一个经验法则,并非 100% 总是正确的。
我知道 shared_ptr
, unique_ptr
, weak_ptr
在相同类型的构造函数中用作 RVR 参数后保证为空,但我不知道标准是否指定了这一点除了我提到的那些之外,还有一些其他 std::
类型。
请注意,我知道移动后的元素处于有效但 未指定 状态,我在这里对指定了哪些类型状态感兴趣。
使 moved-from 对象处于 "empty" 状态的类型是智能指针、锁 ([thread.lock.unique.cons]/21, [thread.lock.shared.cons]/21), file streams ([filebuf.cons]/(4.2)), futures ([futures.unique_future]/(8.2), [futures.shared_future]/10), promises ([futures.promise]/6), packaged tasks ([futures.task]/7), threads ([thread.thread.constr]/10)、...
相比之下,留下 moved-from 个未指定值的对象的模板是 function
([func.wrap.func.con]/6), basic_regex
([re.regex.construct]/13), basic_string
([string.cons]/2),容器…
经验法则
根据经验:move-only 类型或具有共享引用语义的类型将它们的 moved-from 对象留在空状态。 所有其他类型保留未指定的值。
Move-only 类型
Move-only 类型(使 moved-from 对象处于空状态)是 std::unique_lock
、std::thread
、std::promise
、std::packaged_task
、 std::future
、basic_filebuf
、std::basic_ifstream
、std::basic_ofstream
、std::basic_fstream
、std::shared_lock
和 std::unique_ptr
。
共享引用语义
具有共享引用语义的类型是 std::shared_future
,当然还有 std::shared_ptr
和 std::weak_ptr
。它们的 moved-from 对象也处于空状态。
一个值得注意的例外
当我浏览标准库时,我发现 std::stringstream
及其 input-only 和 output-only 兄弟姐妹(std::istringstream
和 std::ostringstream
)是一个值得注意的例外。这些 类 是 move-only,但没有关于 move-construction 上的 moved-from 对象的信息。因此,valid-but-unspecified-rule 适用。如您所见,这只是一个经验法则,并非 100% 总是正确的。