make_unique and perfect forwarding
std::make_shared
isn't just shorthand for std::shared_ptr<Type> ptr(new Type(...));
. It does something that you cannot do without it.
In order to do its job, std::shared_ptr
must allocate a tracking block in addition to holding the storage for the actual pointer. However, because std::make_shared
allocates the actual object, it is possible that std::make_shared
allocates both the object and the tracking block in the same block of memory.
So while std::shared_ptr<Type> ptr = new Type(...);
would be two memory allocations (one for the new
, one in the std::shared_ptr
tracking block), std::make_shared<Type>(...)
would allocate one block of memory.
That is important for many potential users of std::shared_ptr
. The only thing a std::make_unique
would do is be slightly more convenient. Nothing more than that.
Nice, but Stephan T. Lavavej (better known as STL) has a better solution for make_unique
, which works correctly for the array version.
#include <memory>
#include <type_traits>
#include <utility>
template <typename T, typename... Args>
std::unique_ptr<T> make_unique_helper(std::false_type, Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <typename T, typename... Args>
std::unique_ptr<T> make_unique_helper(std::true_type, Args&&... args) {
static_assert(std::extent<T>::value == 0,
"make_unique<T[N]>() is forbidden, please use make_unique<T[]>().");
typedef typename std::remove_extent<T>::type U;
return std::unique_ptr<T>(new U[sizeof...(Args)]{std::forward<Args>(args)...});
}
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return make_unique_helper<T>(std::is_array<T>(), std::forward<Args>(args)...);
}
This can be seen on his Core C++ 6 video.
An updated version of STL's version of make_unique is now available as N3656. This version got adopted into draft C++14.
Herb Sutter, chair of the C++ standardization committee, writes on his blog:
That C++11 doesn’t include
make_unique
is partly an oversight, and it will almost certainly be added in the future.
He also gives an implementation that is identical with the one given by the OP.
Edit: std::make_unique
now is part of C++14.