Skip to content

Instantly share code, notes, and snippets.

@imoldman
Last active December 25, 2015 03:59
Show Gist options
  • Save imoldman/6914121 to your computer and use it in GitHub Desktop.
Save imoldman/6914121 to your computer and use it in GitHub Desktop.
implementation about std::make_unique in c++14 for vc11(vs2012), require boost-preprocessor library
#include <memory>
#include <type_traits>
#include <boost/preprocessor/repetition.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>
#include <boost/preprocessor/comparison/greater.hpp>
namespace base
{
#define _MAKE_UNIQUE_PARAM_TYPENAME(z, n, data) typename ParamT##n
#define _MAKE_UNIQUE_PARAM_DECLARE(z, n, data) ParamT##n&& param##n
#define _MAKE_UNIQUE_PARAM(z, n, data) std::forward<ParamT##n>(param##n)
#define _MKAE_UNIQUE(z, n, data) \
template<typename T \
BOOST_PP_COMMA_IF(BOOST_PP_GREATER(n,0)) \
BOOST_PP_ENUM(n, _MAKE_UNIQUE_PARAM_TYPENAME, 0)> \
typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type\
make_unique(BOOST_PP_ENUM(n, _MAKE_UNIQUE_PARAM_DECLARE, 0)) { \
return std::unique_ptr<T>(new T(BOOST_PP_ENUM(n, _MAKE_UNIQUE_PARAM, 0))); \
}
BOOST_PP_REPEAT(BOOST_PP_ADD(_VARIADIC_MAX, 1), _MKAE_UNIQUE, 0)
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]());
}
#undef _MAKE_UNIQUE_PARAM_TYPENAME
#undef _MAKE_UNIQUE_PARAM_DECLARE
#undef _MAKE_UNIQUE_PARAM
}
@imoldman
Copy link
Author

boost-preprocessor library is here: https://github.com/imoldman/boost-preprocessor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment