diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 34aa934..66409fb 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -8009,34 +8009,7 @@ Avoid resource leaks. ### 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 p {new Foo{7}}; // OK: but repetitive - - auto q = make_unique(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. - // - // If bar throws, Foo will not be destroyed, and the memory-allocated for it will leak. - f(unique_ptr(new Foo()), bar()); - - // Exception-safe: calls to functions are never interleaved. - f(make_unique(), bar()); - -##### Enforcement - -* Flag the repetitive usage of template specialization list `` -* Flag variables declared to be `unique_ptr` +See [R.23](#Rr-make_unique) ### 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 p {new Foo{7}}; // OK: but repetitive + + auto q = make_unique(7); // Better: no repetition of Foo ##### Enforcement