1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2014. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/move for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_MOVE_MAKE_UNIQUE_HPP_INCLUDED
12 #define BOOST_MOVE_MAKE_UNIQUE_HPP_INCLUDED
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/move/detail/config_begin.hpp>
23 #include <boost/move/detail/workaround.hpp>
24 #include <boost/move/utility_core.hpp>
25 #include <boost/move/unique_ptr.hpp>
26 #include <cstddef> //for std::size_t
27 #include <boost/move/detail/unique_ptr_meta_utils.hpp>
28 #ifdef BOOST_NO_CXX11_VARIADIC_TEMPLATES
29 # include <boost/move/detail/fwd_macros.hpp>
33 //! Defines "make_unique" functions, which are factories to create instances
34 //! of unique_ptr depending on the passed arguments.
36 //! This header can be a bit heavyweight in C++03 compilers due to the use of the
37 //! preprocessor library, that's why it's a a separate header from <tt>unique_ptr.hpp</tt>
39 #if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
41 #if defined(_MSC_VER) && (_MSC_VER >= 1915)
42 #pragma warning (push)
43 #pragma warning (disable : 4643) // Forward declaring 'X' in namespace std is not permitted by the C++ Standard
46 namespace std { //no namespace versioning in clang+libc++
52 #if defined(_MSC_VER) && (_MSC_VER >= 1915)
60 //Compile time switch between
61 //single element, unknown bound array
62 //and known bound array
66 typedef ::boost::movelib::unique_ptr<T> t_is_not_array;
70 struct unique_ptr_if<T[]>
72 typedef ::boost::movelib::unique_ptr<T[]> t_is_array_of_unknown_bound;
75 template<class T, std::size_t N>
76 struct unique_ptr_if<T[N]>
78 typedef void t_is_array_of_known_bound;
81 template <int Dummy = 0>
84 static std::nothrow_t *pnothrow;
88 std::nothrow_t *nothrow_holder<Dummy>::pnothrow =
89 reinterpret_cast<std::nothrow_t *>(0x1234); //Avoid reference to null errors in sanitizers
91 } //namespace move_upmu {
94 #endif //!defined(BOOST_MOVE_DOXYGEN_INVOKED)
99 #if defined(BOOST_MOVE_DOXYGEN_INVOKED) || !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
101 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
103 //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::forward<Args>(args)...))</tt>.
104 template<class T, class... Args>
105 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
106 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
107 make_unique(BOOST_FWD_REF(Args)... args)
108 { return unique_ptr<T>(new T(::boost::forward<Args>(args)...)); }
110 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
112 //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)(std::forward<Args>(args)...))</tt>.
113 template<class T, class... Args>
114 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
115 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
116 make_unique_nothrow(BOOST_FWD_REF(Args)... args)
117 { return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T(::boost::forward<Args>(args)...)); }
120 #define BOOST_MOVE_MAKE_UNIQUE_CODE(N)\
121 template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
122 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
123 make_unique( BOOST_MOVE_UREF##N)\
124 { return unique_ptr<T>( new T( BOOST_MOVE_FWD##N ) ); }\
126 template<class T BOOST_MOVE_I##N BOOST_MOVE_CLASS##N>\
127 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array\
128 make_unique_nothrow( BOOST_MOVE_UREF##N)\
129 { return unique_ptr<T>( new (*boost::move_upmu::nothrow_holder<>::pnothrow)T ( BOOST_MOVE_FWD##N ) ); }\
131 BOOST_MOVE_ITERATE_0TO9(BOOST_MOVE_MAKE_UNIQUE_CODE)
132 #undef BOOST_MOVE_MAKE_UNIQUE_CODE
136 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
138 //! <b>Returns</b>: <tt>unique_ptr<T>(new T)</tt> (default initialization)
140 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
141 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
142 make_unique_definit()
144 return unique_ptr<T>(new T);
147 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is not an array.
149 //! <b>Returns</b>: <tt>unique_ptr<T>(new T(std::nothrow)</tt> (default initialization)
151 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
152 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_not_array)
153 make_unique_nothrow_definit()
155 return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)T);
158 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
161 //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n]())</tt> (value initialization)
163 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
164 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
165 make_unique(std::size_t n)
167 typedef typename ::boost::move_upmu::remove_extent<T>::type U;
168 return unique_ptr<T>(new U[n]());
171 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
174 //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n]())</tt> (value initialization)
176 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
177 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
178 make_unique_nothrow(std::size_t n)
180 typedef typename ::boost::move_upmu::remove_extent<T>::type U;
181 return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow)U[n]());
184 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
187 //! <b>Returns</b>: <tt>unique_ptr<T>(new remove_extent_t<T>[n])</tt> (default initialization)
189 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
190 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
191 make_unique_definit(std::size_t n)
193 typedef typename ::boost::move_upmu::remove_extent<T>::type U;
194 return unique_ptr<T>(new U[n]);
197 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is an array of
200 //! <b>Returns</b>: <tt>unique_ptr<T>(new (std::nothrow)remove_extent_t<T>[n])</tt> (default initialization)
202 inline BOOST_MOVE_DOC1ST(unique_ptr<T>,
203 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_unknown_bound)
204 make_unique_nothrow_definit(std::size_t n)
206 typedef typename ::boost::move_upmu::remove_extent<T>::type U;
207 return unique_ptr<T>(new (*boost::move_upmu::nothrow_holder<>::pnothrow) U[n]);
210 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS)
212 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
213 //! an array of known bound.
214 template<class T, class... Args>
215 inline BOOST_MOVE_DOC1ST(unspecified,
216 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
217 make_unique(BOOST_FWD_REF(Args) ...) = delete;
219 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
220 //! an array of known bound.
221 template<class T, class... Args>
222 inline BOOST_MOVE_DOC1ST(unspecified,
223 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
224 make_unique_definit(BOOST_FWD_REF(Args) ...) = delete;
226 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
227 //! an array of known bound.
228 template<class T, class... Args>
229 inline BOOST_MOVE_DOC1ST(unspecified,
230 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
231 make_unique_nothrow(BOOST_FWD_REF(Args) ...) = delete;
233 //! <b>Remarks</b>: This function shall not participate in overload resolution unless T is
234 //! an array of known bound.
235 template<class T, class... Args>
236 inline BOOST_MOVE_DOC1ST(unspecified,
237 typename ::boost::move_upmu::unique_ptr_if<T>::t_is_array_of_known_bound)
238 make_unique_nothrow_definit(BOOST_FWD_REF(Args) ...) = delete;
242 } //namespace movelib {
246 #include <boost/move/detail/config_end.hpp>
248 #endif //#ifndef BOOST_MOVE_MAKE_UNIQUE_HPP_INCLUDED