Exception safety and make_unique

Not only when you have multiple allocations, but whenever you can throw at different places. Consider this: f(make_unique<T>(), function_that_can_throw()); Versus: f(unique_ptr<T>(new T), function_that_can_throw()); In the second case, the compiler is allowed to call (in order): new T function_that_can_throw() unique_ptr<T>(…) Obviously if function_that_can_throw actually throws then you leak. make_unique prevents this case. And of course, a … Read more

The simplest and neatest c++11 ScopeGuard

Even shorter: I don’t know why you guys insist on putting the template on the guard class. #include <functional> class scope_guard { public: template<class Callable> scope_guard(Callable && undo_func) try : f(std::forward<Callable>(undo_func)) { } catch(…) { undo_func(); throw; } scope_guard(scope_guard && other) : f(std::move(other.f)) { other.f = nullptr; } ~scope_guard() { if(f) f(); // must not … Read more