Why is there no safe alternative to unique_ptr::operator*()?
问题 std::vector has the member function at() as a safe alternative to operator[] , so that bound checking is applied and no dangling references are created: void foo(std::vector<int> const&x) { const auto&a=x[0]; // What if x.empty()? Undefined behavior! const auto&a=x.at(0); // Throws exception if x.empty(). } However, std::unique_ptr lacks the corresponding functionality: void foo(std::unique_ptr<int> const&x) { const auto&a=*x; // What if bool(x)==false? Undefined behavior! } It would be great