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 can only be captured by copy and never by reference. Even if you specify only [&], this can be implicitly captured by copy if odr-used. Therefore, [=, this] is an error because = would already implicitly capture this by copy while the & in [&, this] signifies capture by reference and does not implicitly capture this (unless it is odr-used)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With