我用 C++ 编写代码,使用std::unique_ptr u
处理一个std::string
资源,我想取消引用u
这样我就可以通过std::string
的电话std::string
复制构造函数:
std::string* copy = new std::string( /*dereference u here*/ );
我知道new
or the std::string
复制构造函数可能会抛出异常,但这不是我的重点。我只是想知道是否取消引用u
可能已经抛出异常。我觉得奇怪的是operator*
is not marked noexcept
而std::unique_ptr
method get
实际上已标记noexcept
。换句话说:
*( u.get() )
is noexcept
整体而言
*u
不是。这是标准中的缺陷吗?我不明白为什么会有差异。有任何想法吗?
unique_ptr::operator*()
可能涉及致电operator*()
您存储在中的类型的重载unique_ptr
。请注意,存储在 a 中的类型unique_ptr
不必是裸指针,您可以通过嵌套类型更改类型D::pointer
, where D
是的类型unique_ptr
的删除器。这就是为什么该功能不noexcept
.
此警告不适用于您的用例,因为您正在存储std::string *
in the unique_ptr
而不是某种重载的类型operator*
。所以这个调用是有效的noexcept
为你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)