The motivation behind make_unique is primarily two-fold:
-
make_uniqueis safe for creating temporaries, whereas with explicit use ofnewyou have to remember the rule about not using unnamed temporaries.foo(make_unique<T>(), make_unique<U>()); // exception safe foo(unique_ptr<T>(new T()), unique_ptr<U>(new U())); // unsafe* -
The addition of
make_uniquefinally means we can tell people to ‘never’ usenewrather than the previous rule to “‘never’ usenewexcept when you make aunique_ptr“.
There’s also a third reason:
make_uniquedoes not require redundant type usage.unique_ptr<T>(new T())->make_unique<T>()
None of the reasons involve improving runtime efficiency the way using make_shared does (due to avoiding a second allocation, at the cost of potentially higher peak memory usage).
* It is expected that C++17 will include a rule change that means that this is no longer unsafe. See C++ committee papers P0400R0 and P0145R3.