2017-06-12 01:58:08 +03:00
|
|
|
////
|
|
|
|
|
Copyright 2017 Peter Dimov
|
2017-06-14 08:24:31 -04:00
|
|
|
Copyright 2017 Glen Joseph Fernandes (glenjofe@gmail.com)
|
2017-06-12 01:58:08 +03:00
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0.
|
|
|
|
|
|
|
|
|
|
See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
|
http://www.boost.org/LICENSE_1_0.txt
|
|
|
|
|
////
|
|
|
|
|
|
|
|
|
|
[#make_unique]
|
2017-06-12 03:12:03 +03:00
|
|
|
# make_unique: Creating unique_ptr
|
2017-06-12 01:58:08 +03:00
|
|
|
:toc:
|
|
|
|
|
:toc-title:
|
2017-06-12 03:12:03 +03:00
|
|
|
:idprefix: make_unique_
|
2017-06-12 01:58:08 +03:00
|
|
|
|
2017-06-14 08:24:31 -04:00
|
|
|
## Description
|
|
|
|
|
|
|
|
|
|
The `make_unique` function templates provide convenient and safe ways to
|
|
|
|
|
create `std::unique_ptr` objects.
|
|
|
|
|
|
|
|
|
|
## Rationale
|
|
|
|
|
|
2017-06-15 08:16:50 -04:00
|
|
|
The {cpp}11 standard introduced `std::unique_ptr` but did not provide any
|
2017-06-14 08:24:31 -04:00
|
|
|
`make_unique` utility like `std::make_shared` that provided the same
|
|
|
|
|
exception safety and facility to avoid writing `new` expressions. Before it
|
2017-06-15 08:16:50 -04:00
|
|
|
was implemented by some standard library vendors (and prior to the {cpp}14
|
2017-06-14 08:24:31 -04:00
|
|
|
standard introducing `std::make_unique`), this library provided it due to
|
|
|
|
|
requests from users.
|
|
|
|
|
|
|
|
|
|
This library also provides additional overloads of `make_unique` for
|
|
|
|
|
default-initialization, when users do not need or want to incur the expense
|
2017-06-15 08:16:50 -04:00
|
|
|
of value-initialization. The {cpp} standard does not yet provide this
|
2017-06-14 08:24:31 -04:00
|
|
|
feature with `std::make_unique`.
|
|
|
|
|
|
|
|
|
|
## Synopsis
|
|
|
|
|
|
2017-11-02 23:07:16 +01:00
|
|
|
`make_unique` is defined in `<boost/smart_ptr/make_unique.hpp>`.
|
2017-06-14 08:24:31 -04:00
|
|
|
|
|
|
|
|
[subs=+quotes]
|
|
|
|
|
```
|
|
|
|
|
namespace boost {
|
2017-06-14 11:28:09 -04:00
|
|
|
`// only if T is not an array type`
|
2017-06-14 08:24:31 -04:00
|
|
|
template<class T, class... Args>
|
|
|
|
|
std::unique_ptr<T> make_unique(Args&&... args);
|
|
|
|
|
|
2017-06-14 11:28:09 -04:00
|
|
|
`// only if T is not an array type`
|
2017-06-14 08:24:31 -04:00
|
|
|
template<class T>
|
2017-06-14 20:39:49 -04:00
|
|
|
std::unique_ptr<T> make_unique(remove_reference_t<T>&& v);
|
2017-06-14 08:24:31 -04:00
|
|
|
|
2017-06-14 11:28:09 -04:00
|
|
|
`// only if T is an array type of the form U[]`
|
2017-06-14 08:24:31 -04:00
|
|
|
template<class T>
|
2017-06-14 10:27:46 -04:00
|
|
|
std::unique_ptr<T> make_unique(std::size_t n);
|
2017-06-14 08:24:31 -04:00
|
|
|
|
2017-06-14 11:28:09 -04:00
|
|
|
`// only if T is not an array type`
|
2017-06-14 08:24:31 -04:00
|
|
|
template<class T>
|
2017-06-14 10:27:46 -04:00
|
|
|
std::unique_ptr<T> make_unique_noinit();
|
2017-06-14 08:24:31 -04:00
|
|
|
|
2017-06-14 11:28:09 -04:00
|
|
|
`// only if T is an array type of the form U[]`
|
2017-06-14 08:24:31 -04:00
|
|
|
template<class T>
|
2017-06-14 10:27:46 -04:00
|
|
|
std::unique_ptr<T> make_unique_noinit(std::size_t n);
|
2017-06-14 08:24:31 -04:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Free Functions
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
template<class T, class... Args>
|
|
|
|
|
std::unique_ptr<T> make_unique(Args&&... args);
|
|
|
|
|
```
|
|
|
|
|
::
|
|
|
|
|
Remarks::: These overloads shall only participate in overload resolution when
|
|
|
|
|
`T` is not an array type.
|
2017-06-15 08:16:50 -04:00
|
|
|
Returns::: `std::unique_ptr<T>(new T(std::forward<Args>(args)\...)`.
|
2017-06-14 08:24:31 -04:00
|
|
|
Example::: `auto p = make_unique<int>();`
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
template<class T>
|
2017-06-14 20:39:49 -04:00
|
|
|
std::unique_ptr<T> make_unique(remove_reference_t<T>&& v);
|
2017-06-14 08:24:31 -04:00
|
|
|
```
|
|
|
|
|
::
|
2017-06-15 01:36:59 -04:00
|
|
|
Remarks::: These overloads shall only participate in overload resolution when
|
|
|
|
|
`T` is not an array type.
|
2017-06-14 08:24:31 -04:00
|
|
|
Returns::: `std::unique_ptr<T>(new T(std::move(v))`.
|
|
|
|
|
Example::: `auto p = make_unique<std::vector<int> >({1, 2});`
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
template<class T>
|
|
|
|
|
std::unique_ptr<T> make_unique(std::size_t n);
|
|
|
|
|
```
|
|
|
|
|
::
|
|
|
|
|
Remarks::: These overloads shall only participate in overload resolution when
|
2017-06-14 10:27:46 -04:00
|
|
|
`T` is an array type of the form `U[]`.
|
|
|
|
|
Returns::: `std::unique_ptr<U[]>(new U[n]())`.
|
2017-06-14 08:24:31 -04:00
|
|
|
Example::: `auto p = make_unique<double[]>(1024);`
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
template<class T>
|
|
|
|
|
std::unique_ptr<T> make_unique_noinit();
|
|
|
|
|
```
|
|
|
|
|
::
|
|
|
|
|
Remarks::: These overloads shall only participate in overload resolution when
|
|
|
|
|
`T` is not an array type.
|
|
|
|
|
Returns::: `std::unique_ptr<T>(new T)`.
|
|
|
|
|
Example::: `auto p = make_unique_noinit<double[1024]>();`
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
template<class T>
|
|
|
|
|
std::unique_ptr<T> make_unique_noinit(std::size_t n);
|
|
|
|
|
```
|
|
|
|
|
::
|
|
|
|
|
Remarks::: These overloads shall only participate in overload resolution when
|
2017-06-14 10:27:46 -04:00
|
|
|
`T` is an array type of the form `U[]`.
|
|
|
|
|
Returns::: `std::unique_ptr<U[]>(new U[n])`.
|
|
|
|
|
Example::: `auto p = make_unique_noinit<double[]>(1024);`
|