使用智能指针,.get()和 - >之间有区别吗?

iFr*_*cht 1 c++ stl smart-pointers operators c++11

std::shared_ptrstd::unique_ptr功能.get()operator->做的完全一样吗?

或者与std::vectors .at()operator[]?有区别吗?

CB *_*ley 6

具有相同的行为(在两种情况下operator->()都定义为返回get()),但operator->()具有get()必须不返回0 的前提条件.

这意味着:

a.get();        // does not cause UB just because holds a null pointer
a.operator->(); // would cause UB if a.get() == 0
Run Code Online (Sandbox Code Playgroud)

哪里a是a std::unique_ptr或a std::shared_ptr.