make_unique.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/smart_ptr/detail/sp_type_traits.hpp>
  10. #include <memory>
  11. #include <type_traits>
  12. #include <utility>
  13. namespace boost {
  14. template<class T>
  15. inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
  16. make_unique()
  17. {
  18. return std::unique_ptr<T>(new T());
  19. }
  20. template<class T, class... Args>
  21. inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
  22. make_unique(Args&&... args)
  23. {
  24. return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
  25. }
  26. template<class T>
  27. inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
  28. make_unique(typename std::remove_reference<T>::type&& value)
  29. {
  30. return std::unique_ptr<T>(new T(std::move(value)));
  31. }
  32. template<class T>
  33. inline typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T> >::type
  34. make_unique_noinit()
  35. {
  36. return std::unique_ptr<T>(new T);
  37. }
  38. template<class T>
  39. inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
  40. std::unique_ptr<T> >::type
  41. make_unique(std::size_t size)
  42. {
  43. return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
  44. }
  45. template<class T>
  46. inline typename std::enable_if<detail::sp_is_unbounded_array<T>::value,
  47. std::unique_ptr<T> >::type
  48. make_unique_noinit(std::size_t size)
  49. {
  50. return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]);
  51. }
  52. } /* boost */
  53. #endif