make_unique.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. Copyright 2012-2019 Glen Joseph Fernandes
  3. ([email protected])
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_SMART_PTR_MAKE_UNIQUE_HPP
  8. #define BOOST_SMART_PTR_MAKE_UNIQUE_HPP
  9. #include <boost/smart_ptr/detail/requires_cxx11.hpp>
  10. #include <boost/type_traits/enable_if.hpp>
  11. #include <boost/type_traits/is_array.hpp>
  12. #include <boost/type_traits/is_unbounded_array.hpp>
  13. #include <boost/type_traits/remove_extent.hpp>
  14. #include <boost/type_traits/remove_reference.hpp>
  15. #include <memory>
  16. #include <utility>
  17. namespace boost {
  18. template<class T>
  19. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  20. make_unique()
  21. {
  22. return std::unique_ptr<T>(new T());
  23. }
  24. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  25. template<class T, class... Args>
  26. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  27. make_unique(Args&&... args)
  28. {
  29. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  30. }
  31. #endif
  32. template<class T>
  33. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  34. make_unique(typename remove_reference<T>::type&& value)
  35. {
  36. return std::unique_ptr<T>(new T(std::move(value)));
  37. }
  38. template<class T>
  39. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  40. make_unique_noinit()
  41. {
  42. return std::unique_ptr<T>(new T);
  43. }
  44. template<class T>
  45. inline typename enable_if_<is_unbounded_array<T>::value,
  46. std::unique_ptr<T> >::type
  47. make_unique(std::size_t size)
  48. {
  49. return std::unique_ptr<T>(new typename remove_extent<T>::type[size]());
  50. }
  51. template<class T>
  52. inline typename enable_if_<is_unbounded_array<T>::value,
  53. std::unique_ptr<T> >::type
  54. make_unique_noinit(std::size_t size)
  55. {
  56. return std::unique_ptr<T>(new typename remove_extent<T>::type[size]);
  57. }
  58. } /* boost */
  59. #endif