Skip to content

Instantly share code, notes, and snippets.

@imoldman
Created October 10, 2013 06:37
Show Gist options
  • Save imoldman/6914012 to your computer and use it in GitHub Desktop.
Save imoldman/6914012 to your computer and use it in GitHub Desktop.
implementation for std::make_unique in c++14
// usage:
// auto _ = make_unqiue<string>("foobar");
// auto _ = make_unique<string[]>(2);
#include <memory>
#include <type_traits>
namespace base
{
template<typename T, typename... ParamT>
typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
make_unique(ParamT&&... params) {
return std::unique_ptr<T>(new T(std::forward<ParamT>(params)...));
}
template<typename T>
typename std::enable_if<std::is_same<T, typename std::remove_extent<T>::type[]>::value, std::unique_ptr<T>>::type
make_unique(size_t n) {
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment