"Without dereferencing markup" std :: unique_ptr

I am writing code in C ++ that uses std::unique_ptr u to process the std::string resource, and I want to dereference u so that I can pass std::string to the constructor call std::string :

 std::string* copy = new std::string( /*dereference u here*/ ); 

I know that the copy constructor new or std::string can reset, but that is not my point here. I'm just wondering if dereferencing u already throw an exception. It is strange to me that operator* not marked with noexcept , and the std::unique_ptr get method is actually marked with noexcept . In other words:

 *( u.get() ) 

is noexcept in general as well

 *u 

not. Is this a flaw in the standard? I do not understand why there may be a difference. Any ideas?

+7
c ++ c ++ 11 unique-ptr
source share
1 answer

unique_ptr::operator*() may include an operator*() overload call for the type that you store in unique_ptr . Note that the type stored in unique_ptr does not have to be a bare pointer; you can change the type through the nested type D::pointer , where D is the type of the unique_ptr unique_ptr . That is why the function is not noexcept .

This disclaimer does not apply to your use case because you are std::string * in unique_ptr and not some type that overloads operator* . Therefore, the call is effectively noexcept for you.

+9
source share

All Articles