mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2025-12-17 20:54:41 +03:00
Manually merged PR 145 for C.150 improvement
This commit is contained in:
@@ -5873,6 +5873,7 @@ Avoid resource leaks.
|
|||||||
##### Reason
|
##### Reason
|
||||||
|
|
||||||
`make_unique` gives a more concise statement of the construction.
|
`make_unique` gives a more concise statement of the construction.
|
||||||
|
It also ensures exception safety in complex expressions.
|
||||||
|
|
||||||
##### Example
|
##### Example
|
||||||
|
|
||||||
@@ -5880,6 +5881,19 @@ Avoid resource leaks.
|
|||||||
|
|
||||||
auto q = make_unique<Foo>(7); // Better: no repetition of Foo
|
auto q = make_unique<Foo>(7); // Better: no repetition of Foo
|
||||||
|
|
||||||
|
// Not exception-safe: the compiler may 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
|
##### Enforcement
|
||||||
|
|
||||||
* Flag the repetitive usage of template specialization list `<Foo>`
|
* Flag the repetitive usage of template specialization list `<Foo>`
|
||||||
|
|||||||
Reference in New Issue
Block a user