From 866df491816e563e113590c88ab797888572fbb6 Mon Sep 17 00:00:00 2001 From: hsutter Date: Wed, 2 Dec 2015 12:07:43 -0800 Subject: [PATCH] Manually merged PR 145 for C.150 improvement --- CppCoreGuidelines.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index d01c755..bfe23a5 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -5873,6 +5873,7 @@ Avoid resource leaks. ##### Reason `make_unique` gives a more concise statement of the construction. +It also ensures exception safety in complex expressions. ##### Example @@ -5880,6 +5881,19 @@ Avoid resource leaks. auto q = make_unique(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. + // + // 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 ``