I'm wondering why =
capture-default mode prohibits this
in capture-list of C++ lambda expression.
That is,
[=, this]{ }; // error
[&, this]{ }; // OK
This is specified by C++11 5.1.2/8.
- If a lambda-capture includes a capture-default that is &, the identifiers in the lambda-capture shall not be preceded by &.
- If a lambda-capture includes a capture-default that is =, the lambda-capture shall not contain this and each identifier it contains shall be preceded by &.
Q: Is there any reason or background story for this rule?
this
implicitly captured anyway when the capture-default is=
?this
is implicitly captured under both=
and&
capture-default.this
is actually always captured by reference irrespective of the default.