...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The header file <boost/make_unique.hpp> provides overloads of
function template make_unique
for convenient creation of
std::unique_ptr
objects.
namespace boost {
template<class T>
std::unique_ptr<T>
make_unique();
template<class T, class... Args>
std::unique_ptr<T>
make_unique(Args&&... args);
template<class T>
std::unique_ptr<T>
make_unique(T&& value);
template<class T>
std::unique_ptr<T>
make_unique(std::size_t size);
template<class T>
std::unique_ptr<T>
make_unique_noinit();
template<class T>
std::unique_ptr<T>
make_unique_noinit(std::size_t size);
}
template<class T, Args>
std::unique_ptr<T> make_unique(args);
T
(or
E[size]
when T
is E[]
, where
size
is determined from args
as specified by
the concrete overload). The storage is initialized from
args
as specified by the concrete overload. If an exception
is thrown, the functions have no effect.std::unique_ptr
instance that stores and owns the
address of the newly allocated and constructed object.r.get() != 0
, where r
is the return
value.std::bad_alloc
, or an exception thrown from the
initialization of the object.value
, or to T(args...)
, where
args...
is a list of constructor arguments,
make_unique
shall perform this initialization via the
expression new T(value)
or new T(args...)
respectively.T
is specified to be
value-initialized, make_unique
shall perform this
initialization via the expression new T()
.T
is specified to be
default-initialized, make_unique_noinit
shall perform this
initialization via the expression new T
.template<class T, class... Args>
std::unique_ptr<T>
make_unique(Args&&... args);
std::unique_ptr
to an object of type T
,
initialized to std::forward<Args>(args)...
.T
is not an array type.boost::make_unique<double>(1.0);
template<class T>
std::unique_ptr<T>
make_unique(T&& value);
std::unique_ptr
to an object of type T
,
initialized to std::move(value)
.T
is not an array type.boost::make_unique<point>({1.0, -1.0});
template<class T>
std::unique_ptr<T>
make_unique(std::size_t size);
std::unique_ptr
to a value-initialized object of type
E[size]
.T
is of the form E[]
.boost::make_unique<int[]>(8);
template<class T>
std::unique_ptr<T>
make_unique_noinit();
std::unique_ptr
to a default-initialized object of
type T
.T
is not an array type.boost::make_unique_noinit<std::tm>();
template<class T>
std::unique_ptr<T>
make_unique_noinit(std::size_t size);
std::unique_ptr
to a default-initialized object of
type E[size]
.T
is of the form E[]
.boost::make_unique_noinit<char[]>(64);