|
|
|
|
@ -8009,34 +8009,7 @@ Avoid resource leaks.
|
|
|
|
|
|
|
|
|
|
### <a name="Rh-make_unique"></a>C.150: Use `make_unique()` to construct objects owned by `unique_ptr`s
|
|
|
|
|
|
|
|
|
|
##### Reason
|
|
|
|
|
|
|
|
|
|
`make_unique` gives a more concise statement of the construction.
|
|
|
|
|
It also ensures exception safety in complex expressions.
|
|
|
|
|
|
|
|
|
|
##### Example
|
|
|
|
|
|
|
|
|
|
unique_ptr<Foo> p {new Foo{7}}; // OK: but repetitive
|
|
|
|
|
|
|
|
|
|
auto q = make_unique<Foo>(7); // Better: no repetition of Foo
|
|
|
|
|
|
|
|
|
|
// Not exception-safe: the compiler might interleave the computations of arguments as follows:
|
|
|
|
|
//
|
|
|
|
|
// 1. allocate memory for Foo,
|
|
|
|
|
// 2. construct Foo,
|
|
|
|
|
// 3. call bar,
|
|
|
|
|
// 4. construct unique_ptr<Foo>.
|
|
|
|
|
//
|
|
|
|
|
// If bar throws, Foo will not be destroyed, and the memory-allocated for it will leak.
|
|
|
|
|
f(unique_ptr<Foo>(new Foo()), bar());
|
|
|
|
|
|
|
|
|
|
// Exception-safe: calls to functions are never interleaved.
|
|
|
|
|
f(make_unique<Foo>(), bar());
|
|
|
|
|
|
|
|
|
|
##### Enforcement
|
|
|
|
|
|
|
|
|
|
* Flag the repetitive usage of template specialization list `<Foo>`
|
|
|
|
|
* Flag variables declared to be `unique_ptr<Foo>`
|
|
|
|
|
See [R.23](#Rr-make_unique)
|
|
|
|
|
|
|
|
|
|
### <a name="Rh-make_shared"></a>C.151: Use `make_shared()` to construct objects owned by `shared_ptr`s
|
|
|
|
|
|
|
|
|
|
@ -9575,11 +9548,14 @@ The `make_shared()` version mentions `X` only once, so it is usually shorter (as
|
|
|
|
|
|
|
|
|
|
##### Reason
|
|
|
|
|
|
|
|
|
|
For convenience and consistency with `shared_ptr`.
|
|
|
|
|
`make_unique` gives a more concise statement of the construction.
|
|
|
|
|
It also ensures exception safety in complex expressions.
|
|
|
|
|
|
|
|
|
|
##### Note
|
|
|
|
|
##### Example
|
|
|
|
|
|
|
|
|
|
`make_unique()` is C++14, but widely available (as well as simple to write).
|
|
|
|
|
unique_ptr<Foo> p {new Foo{7}}; // OK: but repetitive
|
|
|
|
|
|
|
|
|
|
auto q = make_unique<Foo>(7); // Better: no repetition of Foo
|
|
|
|
|
|
|
|
|
|
##### Enforcement
|
|
|
|
|
|
|
|
|
|
|