std :: unique_ptr and exception security

advertisements

Do I need to wrap std::unique_ptr usage with try/catch in code which should be exception safe?
std::unique_ptr will hold a raw memory block created by ::malloc (with my custom deleter to call ::free).


  1. All of std::unique_ptr's constructors* are noexcept
  2. malloc won't throw any exception on failure... it will just return nullptr.
  3. I believe your deleter's constructors won't throw anything either.

So you don't need to catch anything, since nothing will be thrown.


*: See C++11 ยง20.7.1.2.1 unique_ptr constructors [unique.ptr.single.ctor]