There are some functions in the STL which start with the make_
prefix like std::make_pair
, std::make_shared
, std::make_unique
Although it may be subjective, one main touted benefit for this technique is:
write code against interfaces, not implementations
In essence, function template instantiation performs type deduction based on the arguments you pass, whereas class template instantiation does not. As a consequence, you wouldn't have to pass template arguments as you would when instantiating the class directly.
It should be noted though, that this is not about "saving a few characters", but rather about making your code more general, and avoiding being tied to a concrete type in your function call.
However, this is not always the case, as your std::make_shared
example showed, there are still cases in which you have to pass the type as a template argument. But, as Herb Sutter points out, there are several other advantages when it comes to using std::make_shared
:
You should write for clarity and correctness first, and std::make_shared
achieves both of those (subjective, but I agree)
using std::make_shared
is more efficient, because it allocates your object as well as the shared_ptr
object in one go, allowing for lower allocation overhead and a likely better cache alignment.