From bafb571ade0211236b3ff239967f68cc10675db4 Mon Sep 17 00:00:00 2001 From: Sergey Zubkov Date: Thu, 6 Oct 2022 19:17:44 -0400 Subject: [PATCH] F.53 add notes about capture of this and non-local pointers closes #1816 --- CppCoreGuidelines.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CppCoreGuidelines.md b/CppCoreGuidelines.md index 7dc2ee0..dab5044 100644 --- a/CppCoreGuidelines.md +++ b/CppCoreGuidelines.md @@ -4081,6 +4081,12 @@ Pointers and references to locals shouldn't outlive their scope. Lambdas that ca // always be available for the call. thread_pool.queue_work([=] { process(local); }); +##### Note + +If a non-local pointer must be captured, consider using `unique_ptr`; this handles both lifetime and synchronization. + +If the `this` pointer must be captured, consider using `[*this]` capture, which creates a copy of the entire object. + ##### Enforcement * (Simple) Warn when capture-list contains a reference to a locally declared variable