在move构造函数中用作arg后,保证哪些std类型为空/空 [英] Which std types are guaranteed to be empty/null after being used as arg in move constructor

查看:62
本文介绍了在move构造函数中用作arg后,保证哪些std类型为空/空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道shared_ptrunique_ptrweak_ptr在相同类型的构造函数中用作RVR参数后,一定保证为空,但是我不知道标准是否会为std::之外的其他一些std::类型指定此内容我提到的那些.

I know shared_ptr, unique_ptr, weak_ptr are guaranteed to be empty after used as RVR argument in the constructor of the same type, but I wonder does standard specifies this for some other std:: types beside the ones I mentioned.

请注意,我知道移动后的元素仍处于有效状态,但状态为未指定.我对指定状态类型感兴趣.

Please note that I know that elements after move are left in valid but unspecified state, I am here interested for which types state is specified.

推荐答案

经验法则

根据经验法则:仅移动类型或具有共享引用语义的类型将其从移动对象保留为空状态.所有其他类型均保留未指定的值.

A rule of thumb

As a rule of thumb: The move-only types or types with shared reference semantics leave their moved-from object in an empty state. All other types leave unspecified values.

仅移动类型(将移动的对象保留为空状态)为std::unique_lockstd::threadstd::promisestd::packaged_taskstd::futurebasic_filebufstd::basic_ifstreamstd::basic_fstreamstd::shared_lockstd::unique_ptr.

Move-only types (which leave moved-from objects in an empty state) are 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 and std::unique_ptr.

具有共享引用语义的类型为std::shared_future,当然还有std::shared_ptrstd::weak_ptr.这些会将其移出的对象也保持为空状态.

Types with shared reference semantics are std::shared_future, and of course std::shared_ptr and std::weak_ptr. These leave their moved-from objects also in an empty state.

当我浏览标准库时,我发现std::stringstream及其仅输入和仅输出的同级兄弟(std::istringstreamstd::ostringstream)是一个明显的例外.这些类仅是可移动的,但是在移动构造时未告知有关从中移动的对象的任何信息.因此,适用有效但未指定的规则.如您所见,这只是一个经验法则,并非100%总是正确的.

As I went through the standard library, I found std::stringstream and its input-only and output-only siblings (std::istringstream and std::ostringstream) as a notable exception. These classes are move-only, but nothing is being told about the moved-from object upon move-construction. Therefore, the valid-but-unspecified-rule applies. As you see, it's just a rule of thumb, not 100% always correct.

这篇关于在move构造函数中用作arg后,保证哪些std类型为空/空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆