Add make_unique documentation

This commit is contained in:
Glen Fernandes
2017-06-14 08:24:31 -04:00
parent bdcab9df47
commit 8096b7d324
2 changed files with 99 additions and 4 deletions

View File

@@ -271,10 +271,7 @@ Remarks::: These overloads shall only participate in overload resolution when
`T` is not an array type, or an array type of the `__U__[__N__]`.
Returns::: A `shared_ptr` to a default-initialized object of type `T`, or a
sequence of `_N_` default-initialized objects of type `_U_`, respectively.
Examples:::
* `struct X { double data[1024]; }; +
auto p = make_shared_noinit<X>();`
* `auto p = make_shared_noinit<double[1024]>();`
Example::: `auto p = make_shared_noinit<double[1024]>();`
```
template<class T>

View File

@@ -1,5 +1,6 @@
////
Copyright 2017 Peter Dimov
Copyright 2017 Glen Joseph Fernandes (glenjofe@gmail.com)
Distributed under the Boost Software License, Version 1.0.
@@ -13,3 +14,100 @@ http://www.boost.org/LICENSE_1_0.txt
:toc-title:
:idprefix: make_unique_
## Description
The `make_unique` function templates provide convenient and safe ways to
create `std::unique_ptr` objects.
## Rationale
The C$$++$$11 standard introduced `std::unique_ptr` but did not provide any
`make_unique` utility like `std::make_shared` that provided the same
exception safety and facility to avoid writing `new` expressions. Before it
was implemented by some standard library vendors (and prior to the C$$++$$14
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
of value-initialization. The C$$++$$ standard does not yet provide this
feature with `std::make_unique`.
## Synopsis
`make_unique` is defined in `<boost/smart_ptr/make_unqiue.hpp>`.
[subs=+quotes]
```
namespace boost {
template<class T, class... Args>
std::unique_ptr<T> make_unique(Args&&... args);
template<class T>
std::unique_ptr<T> make_unique(_see below_ v);
template<class T>
std::unique_ptr<T> make_shared(std::size_t n);
template<class T>
std::unique_ptr<T> make_shared_noinit();
template<class T>
std::unique_ptr<T> make_shared_noinit(std::size_t n);
}
```
## 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.
Returns::: `std::unique_ptr<T>(new T(std::forward<Args>(args)$$...$$)`.
Example::: `auto p = make_unique<int>();`
[subs=+quotes]
```
template<class T>
std::unique_ptr<T> make_unique(_see below_ v);
```
::
Remarks:::
* These overloads shall only participate in overload resolution when `T` is
not an array type.
* The type of `v` is `std::remove_reference_t<T>&&`.
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
`T` is an array type of the form `__U__[]`.
Returns::: `std::unique_ptr<__U__[]>(new __U__[n]())`.
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
`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);`