From 4fc0beafc385c3f326b47dbdfaf356ccc1e1200c Mon Sep 17 00:00:00 2001 From: psliwa Date: Wed, 3 Feb 2016 18:27:31 +0100 Subject: [PATCH 1/4] F.53: More comprehensive example and enforcements. --- CppCoreGuidelines.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 33c39ae..925de0e 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3149,18 +3149,27 @@ This is a simple three-stage parallel pipeline. Each `stage` object encapsulates Pointers and references to locals shouldn't outlive their scope. Lambdas that capture by reference are just another place to store a reference to a local object, and shouldn't do so if they (or a copy) outlive the scope. -##### Example +##### Example, bad { - // ... + int local_variable = 42; + background_thread.queue_work([&]{ process(local_variable); }); // Want a reference to local_variable. + // Note, that after program exists this scope, local_variable does no longer exist, + // therefore process() call will have undefined behavior! + } - // a, b, c are local variables - background_thread.queue_work([=]{ process(a, b, c); }); // want copies of a, b, and c +##### Example, good + + { + int local_variable = 42; + background_thread.queue_work([=]{ process(local_variable); }); // Want a copy of local_variable. + // Since a copy of local_variable is made, it will be available at all times for the call. } ##### Enforcement -??? +* **Easy:** Warn, when capture-list contains a reference to a locally declared variable +* **Medium:** Flag, when capture-list contains a reference to a locally declared variable and the lambda is passed to a non-`const` and non-local context ### F.54: If you capture `this`, capture all variables explicitly (no default capture) From fdd91e959a8ef2e758052299aa9930a1a60ded1c Mon Sep 17 00:00:00 2001 From: psliwa Date: Thu, 4 Feb 2016 11:05:16 +0100 Subject: [PATCH 2/4] F.53: Minor fix. --- CppCoreGuidelines.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 925de0e..7a2e4ca 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3152,18 +3152,20 @@ Pointers and references to locals shouldn't outlive their scope. Lambdas that ca ##### Example, bad { - int local_variable = 42; - background_thread.queue_work([&]{ process(local_variable); }); // Want a reference to local_variable. - // Note, that after program exists this scope, local_variable does no longer exist, - // therefore process() call will have undefined behavior! + int local = 42; + thread_pool.queue_work([&]{ process(local); }); // Want a reference to local. + // Note, that after program exists this scope, + // local does no longer exist, + // therefore process() call will have undefined behavior! } ##### Example, good { - int local_variable = 42; - background_thread.queue_work([=]{ process(local_variable); }); // Want a copy of local_variable. - // Since a copy of local_variable is made, it will be available at all times for the call. + int local = 42; + thread_pool.queue_work([=]{ process(local); }); // Want a copy of local. + // Since a copy of local is made, it will be + // available at all times for the call. } ##### Enforcement From f11db25628acf41112832dde0838724367daedae Mon Sep 17 00:00:00 2001 From: psliwa Date: Thu, 4 Feb 2016 11:06:55 +0100 Subject: [PATCH 3/4] Minor improvement. --- CppCoreGuidelines.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 7a2e4ca..88beace 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3153,10 +3153,10 @@ Pointers and references to locals shouldn't outlive their scope. Lambdas that ca { int local = 42; - thread_pool.queue_work([&]{ process(local); }); // Want a reference to local. - // Note, that after program exists this scope, - // local does no longer exist, - // therefore process() call will have undefined behavior! + thread_pool.queue_work([&]{ process(local); }); // Want a reference to local. + // Note, that after program exists this scope, + // local does no longer exist, therefore + // process() call will have undefined behavior! } ##### Example, good From c680191bf96fb8f6bbbab4fccef6e0cfd78bd2eb Mon Sep 17 00:00:00 2001 From: psliwa Date: Wed, 24 Feb 2016 13:37:43 +0100 Subject: [PATCH 4/4] F.53: Fixed after review. --- CppCoreGuidelines.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 88beace..de099e9 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -3154,8 +3154,8 @@ Pointers and references to locals shouldn't outlive their scope. Lambdas that ca { int local = 42; thread_pool.queue_work([&]{ process(local); }); // Want a reference to local. - // Note, that after program exists this scope, - // local does no longer exist, therefore + // Note, that after program exits this scope, + // local no longer exists, therefore // process() call will have undefined behavior! } @@ -3170,8 +3170,8 @@ Pointers and references to locals shouldn't outlive their scope. Lambdas that ca ##### Enforcement -* **Easy:** Warn, when capture-list contains a reference to a locally declared variable -* **Medium:** Flag, when capture-list contains a reference to a locally declared variable and the lambda is passed to a non-`const` and non-local context +* (Simple) Warn when capture-list contains a reference to a locally declared variable +* (Complex) Flag when capture-list contains a reference to a locally declared variable and the lambda is passed to a non-`const` and non-local context ### F.54: If you capture `this`, capture all variables explicitly (no default capture)