CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Rule F.54 not clear about capturing this and the use of [&]

Open pauljansen42 opened this issue 2 years ago • 0 comments

The synopsis of rule F.54 is "When writing a lambda that captures this or any class data member, don’t use [=] default capture". Note that [&] is not mentioned. The enforcement of the rule is "Flag any lambda capture-list that specifies a capture-default of [=] and also captures this (whether explicitly or via the default capture and a use of this in the body)". Still, [&] is not mentioned.

But in the examples

auto lambda = [=] { use(i, x); }; // BAD: "looks like" copy/value capture // [&] has identical semantics and copies the this pointer under the current rules // [=,this] and [&,this] are not much better, and confusing

we see that [&] or [&,this] is not recommended as well.

So my question is, should [&] be added to the synopsis and the enforcement, or should the example be adjusted.

pauljansen42 avatar May 07 '23 19:05 pauljansen42