[=, this]
We propose to allow [=, this]
as a lambda capture.
With the introduction of capture-this
-by-value in C++17, it may be
desirable to increase code readability by being explicit about the kind of capture
in the presence of a [=]
-default:
By contrast, when both [=]
and [=, *this]
are present
in a code base in large numbers, it may be easy to forget that the former is different
from the latter: It is hard to notice the absence of something. In such situations, authors
may wish to be explicit and to spare the reader from having remember the correct default.
The proposal is a pure core language extension. The newly allowed syntax was previously ill-formed.
In section 5.1.5 [expr.prim.lambda], change paragraph 9 as follows.
If a lambda-capture includes a capture-default that is &
,
no identifier in a simple-capture of that lambda-capture shall be preceded
by &
. If a lambda-capture includes a capture-default that
is =
, each simple-capture of that lambda-capture shall be of
the form “&
identifier”, “this
”
or “* this
”. [Note: The form [&, this]
is
redundant but accepted for compatibility with ISO C++ 2014. – end note]
Ignoring appearances in initializers of init-captures, an identifier or this
shall not appear more than once in a lambda-capture. [Example:
– end example]