spe*_*ber 7 c++ unique-ptr c++11
我用C++编写代码,std::unique_ptr u用来处理std::string资源,我想取消引用,u以便我可以传递std::string给std::string复制构造函数的调用:
std::string* copy = new std::string( /*dereference u here*/ );
Run Code Online (Sandbox Code Playgroud)
我知道new或std::string复制构造函数可以抛出,但这不是我的观点.我只是想知道解除引用u是否已经抛出异常.我觉得奇怪,operator*是不是标志着noexcept,而std::unique_ptr方法get实际上是标记noexcept.换一种说法:
*( u.get() )
Run Code Online (Sandbox Code Playgroud)
是noexcept一个整体而言
*u
Run Code Online (Sandbox Code Playgroud)
不是.这是标准中的缺陷吗?我不明白为什么会有区别.有任何想法吗?
unique_ptr::operator*()可能涉及调用operator*()您正在存储的类型的重载unique_ptr.请注意,存储在一个类型中的类型unique_ptr不必是裸指针,您可以通过嵌套类型更改类型D::pointer,其中D是unique_ptr删除器的类型.这就是功能不是的原因noexcept.
因为你存储的这个警告并不适用于您的使用案例std::string *中unique_ptr,而不是某种特定类型的重载operator*.所以这个电话noexcept对你有效.