Created
October 10, 2013 06:37
-
-
Save imoldman/6914012 to your computer and use it in GitHub Desktop.
implementation for std::make_unique in c++14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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