Allow [&] capture default when also capturing this, closes #2060

This commit is contained in:
Herb Sutter 2023-04-13 14:33:21 -07:00
parent 0d70ae927d
commit 3c90d590e1

View File

@ -2385,7 +2385,7 @@ Other function rules:
* [F.51: Where there is a choice, prefer default arguments over overloading](#Rf-default-args)
* [F.52: Prefer capturing by reference in lambdas that will be used locally, including passed to algorithms](#Rf-reference-capture)
* [F.53: Avoid capturing by reference in lambdas that will be used non-locally, including returned, stored on the heap, or passed to another thread](#Rf-value-capture)
* [F.54: If you capture `this`, capture all variables explicitly (no default capture)](#Rf-this-capture)
* [F.54: When writing a lambda that captures `this` or any class data member, don't use `[=]` default capture](#Rf-this-capture)
* [F.55: Don't use `va_arg` arguments](#F-varargs)
* [F.56: Avoid unnecessary condition nesting](#F-nesting)
@ -4118,7 +4118,7 @@ If the `this` pointer must be captured, consider using `[*this]` capture, which
* (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
### <a name="Rf-this-capture"></a>F.54: If you capture `this`, capture all variables explicitly (no default capture)
### <a name="Rf-this-capture"></a>F.54: When writing a lambda that captures `this` or any class data member, don't use `[=]` default capture
##### Reason
@ -4154,11 +4154,11 @@ It's confusing. Writing `[=]` in a member function appears to capture by value,
##### Note
This is under active discussion in standardization, and might be addressed in a future version of the standard by adding a new capture mode or possibly adjusting the meaning of `[=]`. For now, just be explicit.
If you intend to capture a copy of all class data members, consider C++17 `[*this]`.
##### Enforcement
* Flag any lambda capture-list that specifies a capture-default (e.g., `=` or `&`) and also captures `this` (whether explicitly such as `[&, this]` or via default capture such as `[=]` and a use of `this` in the body)
* 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)
### <a name="F-varargs"></a>F.55: Don't use `va_arg` arguments
@ -19760,7 +19760,7 @@ Additions to `std` might clash with future versions of the standard.
};
}
namespace Foo { // GOOD: user namespace is allowed
namespace Foo { // GOOD: user namespace is allowed
class My_vector {
// . . .
};