make_unique.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2012-2019 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  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/type_traits/enable_if.hpp>
  10. #include <boost/type_traits/is_array.hpp>
  11. #include <boost/type_traits/is_unbounded_array.hpp>
  12. #include <boost/type_traits/remove_extent.hpp>
  13. #include <boost/type_traits/remove_reference.hpp>
  14. #include <memory>
  15. #include <utility>
  16. namespace boost {
  17. template<class T>
  18. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  19. make_unique()
  20. {
  21. return std::unique_ptr<T>(new T());
  22. }
  23. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  24. template<class T, class... Args>
  25. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  26. make_unique(Args&&... args)
  27. {
  28. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  29. }
  30. #endif
  31. template<class T>
  32. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  33. make_unique(typename remove_reference<T>::type&& value)
  34. {
  35. return std::unique_ptr<T>(new T(std::move(value)));
  36. }
  37. template<class T>
  38. inline typename enable_if_<!is_array<T>::value, std::unique_ptr<T> >::type
  39. make_unique_noinit()
  40. {
  41. return std::unique_ptr<T>(new T);
  42. }
  43. template<class T>
  44. inline typename enable_if_<is_unbounded_array<T>::value,
  45. std::unique_ptr<T> >::type
  46. make_unique(std::size_t size)
  47. {
  48. return std::unique_ptr<T>(new typename remove_extent<T>::type[size]());
  49. }
  50. template<class T>
  51. inline typename enable_if_<is_unbounded_array<T>::value,
  52. std::unique_ptr<T> >::type
  53. make_unique_noinit(std::size_t size)
  54. {
  55. return std::unique_ptr<T>(new typename remove_extent<T>::type[size]);
  56. }
  57. } /* boost */
  58. #endif