From 840fa592ac926ba844906266845966813e9d50d2 Mon Sep 17 00:00:00 2001 From: Sergey Zubkov Date: Mon, 5 Oct 2020 15:09:08 -0400 Subject: [PATCH] merge C.150 in R.23, leaving a redirect behind (#1676) --- CppCoreGuidelines.md | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) 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 -- GitLab