我使用C++编写代码,其中使用了std::unique_ptr u
来处理std::string
资源,我想解引用u
,以便将std::string
传递给调用std::string
复制构造函数的函数:
std::string* copy = new std::string( /*dereference u here*/ );
我知道
new
或std::string
复制构造函数可能会抛出异常,但这不是我的重点。我只是想知道是否对u
进行解引用操作就已经可以抛出异常。我觉得很奇怪operator*
没有标记为noexcept
,而std::unique_ptr
方法get
实际上被标记为noexcept
。换句话说:*( u.get() )
作为整体而言,
noexcept
是指在运行时不会抛出异常。*u
这是不是一个标准中的缺陷?我不明白为什么会有差异。有任何想法吗?
*(u.get())
怎么可能是noexcept
的呢?难道u.get()
不可能返回nullptr
吗? - BartoszKPnullptr
与noexcept
有什么关系? - Slavaoperator*
可以验证您是否正在取消引用空指针,并在是的情况下抛出异常,而get()
不应该这样做。但是,这仍然没有意义,因为operator->
是noexcept
... - BartoszKP