From 43ca7880c75955aae283877fd0768295e840a170 Mon Sep 17 00:00:00 2001 From: Pascal Cadotte Michaud Date: Fri, 18 Sep 2015 21:03:07 -0400 Subject: [PATCH] remove weird characters in R.38 examples --- CppCoreGuidelines.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 4c5d4bd..69bf08b 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -6223,21 +6223,21 @@ You need to be sure that smart pointer cannot be inadvertently be reset or reass **Example**: Consider this code: - // global (static or heap), or aliased local ... + // global (static or heap), or aliased local... shared_ptr g_p = ...; - - void f( widget& w ) { - g(); - use(w); // A + + void f( widget& w ) { + g(); + use(w); // A } - void g() { - g_p = ... ; // oops, if this was the last shared_ptr to that widget, destroys the widget - } + void g() { + g_p = ... ; // oops, if this was the last shared_ptr to that widget, destroys the widget + } The following should not pass code review: - - void my_code() { + + void my_code() { f( *g_p ); // BAD: passing pointer or reference obtained from a nonlocal smart pointer // that could be inadvertently reset somewhere inside f or it callees g_p->func(); // BAD: same reason, just passing it as a "this" pointer @@ -6246,8 +6246,8 @@ The following should not pass code review: The fix is simple -- take a local copy of the pointer to "keep a ref count" for your call tree: void my_code() { - auto pin = g_p; // cheap: 1 increment covers this entire function and all the call trees below us - f( *pin ); // GOOD: passing pointer or reference obtained from a local unaliased smart pointer + auto pin = g_p; // cheap: 1 increment covers this entire function and all the call trees below us + f( *pin ); // GOOD: passing pointer or reference obtained from a local unaliased smart pointer pin->func(); // GOOD: same reason }