this
is special:
If the capture-default is
=
, subsequent simple captures must begin with&
or be*this
(since C++17) orthis
(since C++20).
normal:
// capture all by value, except teas is by reference
auto func1 = [=, &teas](parameters) -> return-value {
// body
};
// capture all by reference, except banned is by value
auto func2 = [&, banned](parameters) -> return-value {
// body
};
this
:
struct S2 { void f(int i); };
void S2::f(int i)
{
[=] {}; // OK: by-copy capture default
[=, &i] {}; // OK: by-copy capture, except i is captured by reference
[=, *this] {}; // until C++17: Error: invalid syntax
// since C++17: OK: captures the enclosing S2 by copy
[=, this] {}; // until C++20: Error: this when = is the default
// since C++20: OK, same as [=]
}