Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can shared_ptr offer noexcept assignment?

Tags:

How can std::shared_ptr offer a noexcept operator=? Surely, if this shared_ptr is the last one, then it will have to destroy its contents, and it can't guarantee that the destructor of that object does not throw, or the custom deleter used originally does not throw.

like image 714
Puppy Avatar asked Dec 08 '12 14:12

Puppy


2 Answers

Looks like a defect to me, though not one I can find in the active issues list (though #2104 is similar).

  • Per [C++11: 20.7.2.2.3/1], the assignment is defined to be equivalent to shared_ptr(r).swap(*this);

  • But per [C++11: 20.7.2.2.2], ~shared_ptr itself is not noexcept.

Unless I've misunderstood the way in which noexcept works, this must be an error.

Alternatively it could simply mean that the assignment operator is only usable when neither the underlying object type nor the deleter type throw on destruction, though even in such a scenario, the lack of any informative note in the standard wording makes me think that this is unlikely.

like image 199
Lightness Races in Orbit Avatar answered Oct 22 '22 09:10

Lightness Races in Orbit


According to the isocpp forums, shared_ptr simply assumes that the deleter will not throw, and otherwise is UB. This would mean that the real defect is that shared_ptr's destructor is not marked as nothrow.

like image 39
Puppy Avatar answered Oct 22 '22 11:10

Puppy