- tmp/tmpse1mb017/{from.md → to.md} +433 -193
tmp/tmpse1mb017/{from.md → to.md}
RENAMED
|
@@ -1,119 +1,99 @@
|
|
| 1 |
### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
|
| 2 |
|
| 3 |
-
Lambda expressions provide a concise way to create simple function
|
| 4 |
-
objects.
|
| 5 |
-
|
| 6 |
-
``` cpp
|
| 7 |
-
#include <algorithm>
|
| 8 |
-
#include <cmath>
|
| 9 |
-
void abssort(float* x, unsigned N) {
|
| 10 |
-
std::sort(x, x + N,
|
| 11 |
-
[](float a, float b) {
|
| 12 |
-
return std::abs(a) < std::abs(b);
|
| 13 |
-
});
|
| 14 |
-
}
|
| 15 |
-
```
|
| 16 |
-
|
| 17 |
``` bnf
|
| 18 |
lambda-expression:
|
| 19 |
lambda-introducer lambda-declaratorₒₚₜ compound-statement
|
| 20 |
```
|
| 21 |
|
| 22 |
``` bnf
|
| 23 |
lambda-introducer:
|
| 24 |
'[' lambda-captureₒₚₜ ']'
|
| 25 |
```
|
| 26 |
|
| 27 |
-
``` bnf
|
| 28 |
-
lambda-capture:
|
| 29 |
-
capture-default
|
| 30 |
-
capture-list
|
| 31 |
-
capture-default ',' capture-list
|
| 32 |
-
```
|
| 33 |
-
|
| 34 |
-
``` bnf
|
| 35 |
-
capture-default:
|
| 36 |
-
'&'
|
| 37 |
-
'='
|
| 38 |
-
```
|
| 39 |
-
|
| 40 |
-
``` bnf
|
| 41 |
-
capture-list:
|
| 42 |
-
capture '...'ₒₚₜ
|
| 43 |
-
capture-list ',' capture '...'ₒₚₜ
|
| 44 |
-
```
|
| 45 |
-
|
| 46 |
-
``` bnf
|
| 47 |
-
capture:
|
| 48 |
-
simple-capture
|
| 49 |
-
init-capture
|
| 50 |
-
```
|
| 51 |
-
|
| 52 |
-
``` bnf
|
| 53 |
-
simple-capture:
|
| 54 |
-
identifier
|
| 55 |
-
'&' identifier
|
| 56 |
-
'this'
|
| 57 |
-
```
|
| 58 |
-
|
| 59 |
-
``` bnf
|
| 60 |
-
init-capture:
|
| 61 |
-
identifier initializer
|
| 62 |
-
'&' identifier initializer
|
| 63 |
-
```
|
| 64 |
-
|
| 65 |
``` bnf
|
| 66 |
lambda-declarator:
|
| 67 |
-
'(' parameter-declaration-clause ')'
|
| 68 |
-
|
| 69 |
```
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
[
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
*lambda-expression*
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
- the size and/or alignment of the closure type,
|
| 94 |
- whether the closure type is trivially copyable (Clause [[class]]),
|
| 95 |
- whether the closure type is a standard-layout class (Clause
|
| 96 |
[[class]]), or
|
| 97 |
- whether the closure type is a POD class (Clause [[class]]).
|
| 98 |
|
| 99 |
An implementation shall not add members of rvalue reference type to the
|
| 100 |
closure type.
|
| 101 |
|
| 102 |
-
If a *lambda-expression* does not include a *lambda-declarator*, it is
|
| 103 |
-
as if the *lambda-declarator* were `()`. The lambda return type is
|
| 104 |
-
`auto`, which is replaced by the *trailing-return-type* if provided
|
| 105 |
-
and/or deduced from `return` statements as described in
|
| 106 |
-
[[dcl.spec.auto]].
|
| 107 |
-
|
| 108 |
-
``` cpp
|
| 109 |
-
auto x1 = [](int i){ return i; }; // OK: return type is int
|
| 110 |
-
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 111 |
-
int j;
|
| 112 |
-
auto x3 = []()->auto&& { return j; }; // OK: return type is int&
|
| 113 |
-
```
|
| 114 |
-
|
| 115 |
The closure type for a non-generic *lambda-expression* has a public
|
| 116 |
inline function call operator ([[over.call]]) whose parameters and
|
| 117 |
return type are described by the *lambda-expression*’s
|
| 118 |
*parameter-declaration-clause* and *trailing-return-type* respectively.
|
| 119 |
For a generic lambda, the closure type has a public inline function call
|
|
@@ -127,13 +107,16 @@ of the function call operator template are derived from the
|
|
| 127 |
*lambda-expression*'s *trailing-return-type* and
|
| 128 |
*parameter-declaration-clause* by replacing each occurrence of `auto` in
|
| 129 |
the *decl-specifier*s of the *parameter-declaration-clause* with the
|
| 130 |
name of the corresponding invented *template-parameter*.
|
| 131 |
|
|
|
|
|
|
|
| 132 |
``` cpp
|
| 133 |
auto glambda = [](auto a, auto&& b) { return a < b; };
|
| 134 |
bool b = glambda(3, 3.14); // OK
|
|
|
|
| 135 |
auto vglambda = [](auto printer) {
|
| 136 |
return [=](auto&& ... ts) { // OK: ts is a function parameter pack
|
| 137 |
printer(std::forward<decltype(ts)>(ts)...);
|
| 138 |
|
| 139 |
return [=]() {
|
|
@@ -145,42 +128,99 @@ auto glambda = [](auto a, auto&& b) { return a < b; };
|
|
| 145 |
{ std::cout << v1 << v2 << v3; } );
|
| 146 |
auto q = p(1, 'a', 3.14); // OK: outputs 1a3.14
|
| 147 |
q(); // OK: outputs 1a3.14
|
| 148 |
```
|
| 149 |
|
| 150 |
-
|
|
|
|
|
|
|
| 151 |
[[class.mfct.non-static]]) if and only if the *lambda-expression*’s
|
| 152 |
*parameter-declaration-clause* is not followed by `mutable`. It is
|
| 153 |
-
neither virtual nor declared `volatile`. Any *
|
| 154 |
specified on a *lambda-expression* applies to the corresponding function
|
| 155 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 156 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 157 |
-
call operator or operator template.
|
| 158 |
-
|
| 159 |
-
*lambda-expression*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
The closure type for a non-generic *lambda-expression* with no
|
| 162 |
-
*lambda-capture* has a
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
| 171 |
*template-parameter-list*, and the pointer to function has the same
|
| 172 |
parameter types, as the function call operator template. The return type
|
| 173 |
of the pointer to function shall behave as if it were a
|
| 174 |
*decltype-specifier* denoting the return type of the corresponding
|
| 175 |
-
function call operator template specialization.
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
``` cpp
|
| 184 |
auto glambda = [](auto a) { return a; };
|
| 185 |
int (*fp)(int) = glambda;
|
| 186 |
```
|
|
@@ -202,10 +242,14 @@ struct Closure {
|
|
| 202 |
template<class T> operator fptr_t<T>() const
|
| 203 |
{ return &lambda_call_operator_invoker; }
|
| 204 |
};
|
| 205 |
```
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
``` cpp
|
| 208 |
void f1(int (*)(int)) { }
|
| 209 |
void f2(char (*)(int)) { }
|
| 210 |
|
| 211 |
void g(int (*)(int)) { } // #1
|
|
@@ -220,33 +264,63 @@ f2(glambda); // error: ID is not convertible
|
|
| 220 |
g(glambda); // error: ambiguous
|
| 221 |
h(glambda); // OK: calls #3 since it is convertible from ID
|
| 222 |
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
| 223 |
```
|
| 224 |
|
|
|
|
|
|
|
| 225 |
The value returned by any given specialization of this conversion
|
| 226 |
-
function template
|
| 227 |
has the same effect as invoking the generic lambda’s corresponding
|
| 228 |
-
function call operator template specialization.
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
``` cpp
|
| 234 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 235 |
int (*GL_int)(int) = GL; // OK: through conversion function template
|
| 236 |
GL_int(3); // OK: same as GL(3)
|
| 237 |
```
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
The *lambda-expression*’s *compound-statement* yields the
|
| 240 |
*function-body* ([[dcl.fct.def]]) of the function call operator, but
|
| 241 |
for purposes of name lookup ([[basic.lookup]]), determining the type
|
| 242 |
and value of `this` ([[class.this]]) and transforming *id-expression*s
|
| 243 |
referring to non-static class members into class member access
|
| 244 |
expressions using `(*this)` ([[class.mfct.non-static]]), the
|
| 245 |
*compound-statement* is considered in the context of the
|
| 246 |
*lambda-expression*.
|
| 247 |
|
|
|
|
|
|
|
| 248 |
``` cpp
|
| 249 |
struct S1 {
|
| 250 |
int x, y;
|
| 251 |
int operator()(int);
|
| 252 |
void f() {
|
|
@@ -256,46 +330,135 @@ struct S1 {
|
|
| 256 |
};
|
| 257 |
}
|
| 258 |
};
|
| 259 |
```
|
| 260 |
|
|
|
|
|
|
|
| 261 |
Further, a variable `__func__` is implicitly defined at the beginning of
|
| 262 |
the *compound-statement* of the *lambda-expression*, with semantics as
|
| 263 |
described in [[dcl.fct.def.general]].
|
| 264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 266 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 267 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 268 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
| 269 |
-
form “`&` *identifier*”
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
``` cpp
|
| 274 |
struct S2 { void f(int i); };
|
| 275 |
void S2::f(int i) {
|
| 276 |
[&, i]{ }; // OK
|
| 277 |
[&, &i]{ }; // error: i preceded by & when & is the default
|
|
|
|
| 278 |
[=, this]{ }; // error: this when = is the default
|
| 279 |
[i, i]{ }; // error: i repeated
|
|
|
|
| 280 |
}
|
| 281 |
```
|
| 282 |
|
|
|
|
|
|
|
| 283 |
A *lambda-expression* whose smallest enclosing scope is a block scope (
|
| 284 |
[[basic.scope.block]]) is a *local lambda expression*; any other
|
| 285 |
*lambda-expression* shall not have a *capture-default* or
|
| 286 |
*simple-capture* in its *lambda-introducer*. The *reaching scope* of a
|
| 287 |
local lambda expression is the set of enclosing scopes up to and
|
| 288 |
-
including the innermost enclosing function and its parameters.
|
| 289 |
-
|
|
|
|
|
|
|
| 290 |
|
| 291 |
The *identifier* in a *simple-capture* is looked up using the usual
|
| 292 |
rules for unqualified name lookup ([[basic.lookup.unqual]]); each such
|
| 293 |
lookup shall find an entity. An entity that is designated by a
|
| 294 |
*simple-capture* is said to be *explicitly captured*, and shall be
|
| 295 |
-
`this`
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
An *init-capture* behaves as if it declares and explicitly captures a
|
| 299 |
variable of the form “`auto` *init-capture* `;`” whose declarative
|
| 300 |
region is the *lambda-expression*’s *compound-statement*, except that:
|
| 301 |
|
|
@@ -305,34 +468,45 @@ region is the *lambda-expression*’s *compound-statement*, except that:
|
|
| 305 |
non-static data member, and no additional copy and destruction is
|
| 306 |
performed, and
|
| 307 |
- if the capture is by reference, the variable’s lifetime ends when the
|
| 308 |
closure object’s lifetime ends.
|
| 309 |
|
| 310 |
-
This enables an *init-capture* like “`x = std::move(x)`”;
|
| 311 |
-
“`x`” must bind to a declaration in the surrounding
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
``` cpp
|
| 314 |
int x = 4;
|
| 315 |
auto y = [&r = x, x = x+1]()->int {
|
| 316 |
r += 2;
|
| 317 |
return x+2;
|
| 318 |
}(); // Updates ::x to 6, and initializes y to 7.
|
|
|
|
|
|
|
| 319 |
```
|
| 320 |
|
|
|
|
|
|
|
| 321 |
A *lambda-expression* with an associated *capture-default* that does not
|
| 322 |
-
explicitly capture `this` or a variable with automatic storage duration
|
| 323 |
(this excludes any *id-expression* that has been found to refer to an
|
| 324 |
*init-capture*'s associated non-static data member), is said to
|
| 325 |
-
*implicitly capture* the entity (i.e., `this` or a variable) if the
|
| 326 |
*compound-statement*:
|
| 327 |
|
| 328 |
-
- odr-uses ([[basic.def.odr]]) the entity
|
|
|
|
|
|
|
| 329 |
- names the entity in a potentially-evaluated expression (
|
| 330 |
[[basic.def.odr]]) where the enclosing full-expression depends on a
|
| 331 |
generic lambda parameter declared within the reaching scope of the
|
| 332 |
*lambda-expression*.
|
| 333 |
|
|
|
|
|
|
|
| 334 |
``` cpp
|
| 335 |
void f(int, const int (&)[2] = {}) { } // #1
|
| 336 |
void f(const int&, const int (&)[1]) { } // #2
|
| 337 |
void test() {
|
| 338 |
const int x = 17;
|
|
@@ -345,63 +519,85 @@ void test() {
|
|
| 345 |
f(x, selector); // OK: is a dependent expression, so captures x
|
| 346 |
};
|
| 347 |
}
|
| 348 |
```
|
| 349 |
|
|
|
|
|
|
|
| 350 |
All such implicitly captured entities shall be declared within the
|
| 351 |
-
reaching scope of the lambda expression.
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
|
|
|
|
|
|
| 355 |
|
| 356 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 357 |
entity captured by a *lambda-expression* is odr-used (
|
| 358 |
[[basic.def.odr]]) in the scope containing the *lambda-expression*. If
|
| 359 |
-
`this` is captured by a local lambda expression, its nearest enclosing
|
| 360 |
function shall be a non-static member function. If a *lambda-expression*
|
| 361 |
or an instantiation of the function call operator template of a generic
|
| 362 |
lambda odr-uses ([[basic.def.odr]]) `this` or a variable with automatic
|
| 363 |
storage duration from its reaching scope, that entity shall be captured
|
| 364 |
by the *lambda-expression*. If a *lambda-expression* captures an entity
|
| 365 |
and that entity is not defined or captured in the immediately enclosing
|
| 366 |
lambda expression or function, the program is ill-formed.
|
| 367 |
|
|
|
|
|
|
|
| 368 |
``` cpp
|
| 369 |
void f1(int i) {
|
| 370 |
int const N = 20;
|
| 371 |
auto m1 = [=]{
|
| 372 |
int const M = 30;
|
| 373 |
auto m2 = [i]{
|
| 374 |
int x[N][M]; // OK: N and M are not odr-used
|
| 375 |
-
x[0][0] = i; // OK: i is explicitly captured by m2
|
| 376 |
-
// and implicitly captured by m1
|
| 377 |
};
|
| 378 |
};
|
| 379 |
struct s1 {
|
| 380 |
int f;
|
| 381 |
void work(int n) {
|
| 382 |
int m = n*n;
|
| 383 |
int j = 40;
|
| 384 |
auto m3 = [this,m] {
|
| 385 |
auto m4 = [&,j] { // error: j not captured by m3
|
| 386 |
-
int x = n; // error: n implicitly captured by m4
|
| 387 |
-
|
| 388 |
-
x += m; // OK: m implicitly captured by m4
|
| 389 |
-
// and explicitly captured by m3
|
| 390 |
x += i; // error: i is outside of the reaching scope
|
| 391 |
-
x += f; // OK: this captured implicitly by m4
|
| 392 |
-
// and explicitly by m3
|
| 393 |
};
|
| 394 |
};
|
| 395 |
}
|
| 396 |
};
|
| 397 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
```
|
| 399 |
|
|
|
|
|
|
|
| 400 |
A *lambda-expression* appearing in a default argument shall not
|
| 401 |
implicitly or explicitly capture any entity.
|
| 402 |
|
|
|
|
|
|
|
| 403 |
``` cpp
|
| 404 |
void f2() {
|
| 405 |
int i = 1;
|
| 406 |
void g1(int = ([i]{ return i; })()); // ill-formed
|
| 407 |
void g2(int = ([i]{ return 0; })()); // ill-formed
|
|
@@ -409,38 +605,105 @@ void f2() {
|
|
| 409 |
void g4(int = ([=]{ return 0; })()); // OK
|
| 410 |
void g5(int = ([]{ return sizeof i; })()); // OK
|
| 411 |
}
|
| 412 |
```
|
| 413 |
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
An entity is *captured by reference* if it is implicitly or explicitly
|
| 427 |
captured but not captured by copy. It is unspecified whether additional
|
| 428 |
unnamed non-static data members are declared in the closure type for
|
| 429 |
-
entities captured by reference.
|
| 430 |
-
be
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 433 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 434 |
`m2`’s capture is transformed as follows:
|
| 435 |
|
| 436 |
- if `m1` captures the entity by copy, `m2` captures the corresponding
|
| 437 |
non-static data member of `m1`’s closure type;
|
| 438 |
- if `m1` captures the entity by reference, `m2` captures the same
|
| 439 |
entity captured by `m1`.
|
| 440 |
|
| 441 |
-
|
|
|
|
|
|
|
| 442 |
`123234`.
|
| 443 |
|
| 444 |
``` cpp
|
| 445 |
int a = 1, b = 1, c = 1;
|
| 446 |
auto m1 = [a, &b, &c]() mutable {
|
|
@@ -454,86 +717,63 @@ auto m1 = [a, &b, &c]() mutable {
|
|
| 454 |
a = 2; b = 2; c = 2;
|
| 455 |
m1();
|
| 456 |
std::cout << a << b << c;
|
| 457 |
```
|
| 458 |
|
| 459 |
-
|
| 460 |
-
*lambda-expression* that is an odr-use ([[basic.def.odr]]) of an entity
|
| 461 |
-
captured by copy is transformed into an access to the corresponding
|
| 462 |
-
unnamed data member of the closure type. An *id-expression* that is not
|
| 463 |
-
an odr-use refers to the original entity, never to a member of the
|
| 464 |
-
closure type. Furthermore, such an *id-expression* does not cause the
|
| 465 |
-
implicit capture of the entity. If `this` is captured, each odr-use of
|
| 466 |
-
`this` is transformed into an access to the corresponding unnamed data
|
| 467 |
-
member of the closure type, cast ([[expr.cast]]) to the type of `this`.
|
| 468 |
-
The cast ensures that the transformed expression is a prvalue.
|
| 469 |
-
|
| 470 |
-
``` cpp
|
| 471 |
-
void f(const int*);
|
| 472 |
-
void g() {
|
| 473 |
-
const int N = 10;
|
| 474 |
-
[=] {
|
| 475 |
-
int arr[N]; // OK: not an odr-use, refers to automatic variable
|
| 476 |
-
f(&N); // OK: causes N to be captured; &N points to the
|
| 477 |
-
// corresponding member of the closure type
|
| 478 |
-
};
|
| 479 |
-
}
|
| 480 |
-
```
|
| 481 |
|
| 482 |
Every occurrence of `decltype((x))` where `x` is a possibly
|
| 483 |
parenthesized *id-expression* that names an entity of automatic storage
|
| 484 |
duration is treated as if `x` were transformed into an access to a
|
| 485 |
corresponding data member of the closure type that would have been
|
| 486 |
declared if `x` were an odr-use of the denoted entity.
|
| 487 |
|
|
|
|
|
|
|
| 488 |
``` cpp
|
| 489 |
void f3() {
|
| 490 |
float x, &r = x;
|
| 491 |
[=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
|
| 492 |
decltype(x) y1; // y1 has type float
|
| 493 |
-
decltype((x)) y2 = y1; // y2 has type float const& because this lambda
|
| 494 |
-
// is not mutable and x is an lvalue
|
| 495 |
decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
|
| 496 |
decltype((r)) r2 = y2; // r2 has type float const&
|
| 497 |
};
|
| 498 |
}
|
| 499 |
```
|
| 500 |
|
| 501 |
-
|
| 502 |
-
[[dcl.fct.def.delete]]) default constructor and a deleted copy
|
| 503 |
-
assignment operator. It has an implicitly-declared copy constructor (
|
| 504 |
-
[[class.copy]]) and may have an implicitly-declared move constructor (
|
| 505 |
-
[[class.copy]]). The copy/move constructor is implicitly defined in the
|
| 506 |
-
same way as any other implicitly declared copy/move constructor would be
|
| 507 |
-
implicitly defined.
|
| 508 |
-
|
| 509 |
-
The closure type associated with a *lambda-expression* has an
|
| 510 |
-
implicitly-declared destructor ([[class.dtor]]).
|
| 511 |
|
| 512 |
When the *lambda-expression* is evaluated, the entities that are
|
| 513 |
captured by copy are used to direct-initialize each corresponding
|
| 514 |
non-static data member of the resulting closure object, and the
|
| 515 |
non-static data members corresponding to the *init-capture*s are
|
| 516 |
initialized as indicated by the corresponding *initializer* (which may
|
| 517 |
be copy- or direct-initialization). (For array members, the array
|
| 518 |
elements are direct-initialized in increasing subscript order.) These
|
| 519 |
initializations are performed in the (unspecified) order in which the
|
| 520 |
-
non-static data members are declared.
|
| 521 |
-
will occur in the reverse order of the constructions.
|
| 522 |
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
|
|
|
|
|
|
|
|
|
| 527 |
|
| 528 |
A *simple-capture* followed by an ellipsis is a pack expansion (
|
| 529 |
[[temp.variadic]]). An *init-capture* followed by an ellipsis is
|
| 530 |
ill-formed.
|
| 531 |
|
|
|
|
|
|
|
| 532 |
``` cpp
|
| 533 |
template<class... Args>
|
| 534 |
void f(Args... args) {
|
| 535 |
auto lm = [&, args...] { return g(args...); };
|
| 536 |
lm();
|
| 537 |
}
|
| 538 |
```
|
| 539 |
|
|
|
|
|
|
|
|
|
| 1 |
### Lambda expressions <a id="expr.prim.lambda">[[expr.prim.lambda]]</a>
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
``` bnf
|
| 4 |
lambda-expression:
|
| 5 |
lambda-introducer lambda-declaratorₒₚₜ compound-statement
|
| 6 |
```
|
| 7 |
|
| 8 |
``` bnf
|
| 9 |
lambda-introducer:
|
| 10 |
'[' lambda-captureₒₚₜ ']'
|
| 11 |
```
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
``` bnf
|
| 14 |
lambda-declarator:
|
| 15 |
+
'(' parameter-declaration-clause ')' decl-specifier-seqₒₚₜ
|
| 16 |
+
noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 17 |
```
|
| 18 |
|
| 19 |
+
Lambda expressions provide a concise way to create simple function
|
| 20 |
+
objects.
|
| 21 |
+
|
| 22 |
+
[*Example 1*:
|
| 23 |
+
|
| 24 |
+
``` cpp
|
| 25 |
+
#include <algorithm>
|
| 26 |
+
#include <cmath>
|
| 27 |
+
void abssort(float* x, unsigned N) {
|
| 28 |
+
std::sort(x, x + N, [](float a, float b) { return std::abs(a) < std::abs(b); });
|
| 29 |
+
}
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
— *end example*]
|
| 33 |
+
|
| 34 |
+
A *lambda-expression* is a prvalue whose result object is called the
|
| 35 |
+
*closure object*. A *lambda-expression* shall not appear in an
|
| 36 |
+
unevaluated operand (Clause [[expr]]), in a *template-argument*, in an
|
| 37 |
+
*alias-declaration*, in a typedef declaration, or in the declaration of
|
| 38 |
+
a function or function template outside its function body and default
|
| 39 |
+
arguments.
|
| 40 |
+
|
| 41 |
+
[*Note 1*: The intention is to prevent lambdas from appearing in a
|
| 42 |
+
signature. — *end note*]
|
| 43 |
+
|
| 44 |
+
[*Note 2*: A closure object behaves like a function object (
|
| 45 |
+
[[function.objects]]). — *end note*]
|
| 46 |
+
|
| 47 |
+
In the *decl-specifier-seq* of the *lambda-declarator*, each
|
| 48 |
+
*decl-specifier* shall either be `mutable` or `constexpr`.
|
| 49 |
+
|
| 50 |
+
If a *lambda-expression* does not include a *lambda-declarator*, it is
|
| 51 |
+
as if the *lambda-declarator* were `()`. The lambda return type is
|
| 52 |
+
`auto`, which is replaced by the type specified by the
|
| 53 |
+
*trailing-return-type* if provided and/or deduced from `return`
|
| 54 |
+
statements as described in [[dcl.spec.auto]].
|
| 55 |
+
|
| 56 |
+
[*Example 2*:
|
| 57 |
+
|
| 58 |
+
``` cpp
|
| 59 |
+
auto x1 = [](int i){ return i; }; // OK: return type is int
|
| 60 |
+
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 61 |
+
int j;
|
| 62 |
+
auto x3 = []()->auto&& { return j; }; // OK: return type is int&
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
— *end example*]
|
| 66 |
+
|
| 67 |
+
#### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
|
| 68 |
+
|
| 69 |
+
The type of a *lambda-expression* (which is also the type of the closure
|
| 70 |
+
object) is a unique, unnamed non-union class type, called the *closure
|
| 71 |
+
type*, whose properties are described below.
|
| 72 |
+
|
| 73 |
+
The closure type is declared in the smallest block scope, class scope,
|
| 74 |
+
or namespace scope that contains the corresponding *lambda-expression*.
|
| 75 |
+
|
| 76 |
+
[*Note 1*: This determines the set of namespaces and classes associated
|
| 77 |
+
with the closure type ([[basic.lookup.argdep]]). The parameter types of
|
| 78 |
+
a *lambda-declarator* do not affect these associated namespaces and
|
| 79 |
+
classes. — *end note*]
|
| 80 |
+
|
| 81 |
+
The closure type is not an aggregate type ([[dcl.init.aggr]]). An
|
| 82 |
+
implementation may define the closure type differently from what is
|
| 83 |
+
described below provided this does not alter the observable behavior of
|
| 84 |
+
the program other than by changing:
|
| 85 |
|
| 86 |
- the size and/or alignment of the closure type,
|
| 87 |
- whether the closure type is trivially copyable (Clause [[class]]),
|
| 88 |
- whether the closure type is a standard-layout class (Clause
|
| 89 |
[[class]]), or
|
| 90 |
- whether the closure type is a POD class (Clause [[class]]).
|
| 91 |
|
| 92 |
An implementation shall not add members of rvalue reference type to the
|
| 93 |
closure type.
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
The closure type for a non-generic *lambda-expression* has a public
|
| 96 |
inline function call operator ([[over.call]]) whose parameters and
|
| 97 |
return type are described by the *lambda-expression*’s
|
| 98 |
*parameter-declaration-clause* and *trailing-return-type* respectively.
|
| 99 |
For a generic lambda, the closure type has a public inline function call
|
|
|
|
| 107 |
*lambda-expression*'s *trailing-return-type* and
|
| 108 |
*parameter-declaration-clause* by replacing each occurrence of `auto` in
|
| 109 |
the *decl-specifier*s of the *parameter-declaration-clause* with the
|
| 110 |
name of the corresponding invented *template-parameter*.
|
| 111 |
|
| 112 |
+
[*Example 1*:
|
| 113 |
+
|
| 114 |
``` cpp
|
| 115 |
auto glambda = [](auto a, auto&& b) { return a < b; };
|
| 116 |
bool b = glambda(3, 3.14); // OK
|
| 117 |
+
|
| 118 |
auto vglambda = [](auto printer) {
|
| 119 |
return [=](auto&& ... ts) { // OK: ts is a function parameter pack
|
| 120 |
printer(std::forward<decltype(ts)>(ts)...);
|
| 121 |
|
| 122 |
return [=]() {
|
|
|
|
| 128 |
{ std::cout << v1 << v2 << v3; } );
|
| 129 |
auto q = p(1, 'a', 3.14); // OK: outputs 1a3.14
|
| 130 |
q(); // OK: outputs 1a3.14
|
| 131 |
```
|
| 132 |
|
| 133 |
+
— *end example*]
|
| 134 |
+
|
| 135 |
+
The function call operator or operator template is declared `const` (
|
| 136 |
[[class.mfct.non-static]]) if and only if the *lambda-expression*’s
|
| 137 |
*parameter-declaration-clause* is not followed by `mutable`. It is
|
| 138 |
+
neither virtual nor declared `volatile`. Any *noexcept-specifier*
|
| 139 |
specified on a *lambda-expression* applies to the corresponding function
|
| 140 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 141 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 142 |
+
call operator or operator template. The function call operator or any
|
| 143 |
+
given operator template specialization is a constexpr function if either
|
| 144 |
+
the corresponding *lambda-expression*'s *parameter-declaration-clause*
|
| 145 |
+
is followed by `constexpr`, or it satisfies the requirements for a
|
| 146 |
+
constexpr function ([[dcl.constexpr]]).
|
| 147 |
+
|
| 148 |
+
[*Note 2*: Names referenced in the *lambda-declarator* are looked up in
|
| 149 |
+
the context in which the *lambda-expression* appears. — *end note*]
|
| 150 |
+
|
| 151 |
+
[*Example 2*:
|
| 152 |
+
|
| 153 |
+
``` cpp
|
| 154 |
+
auto ID = [](auto a) { return a; };
|
| 155 |
+
static_assert(ID(3) == 3); // OK
|
| 156 |
+
|
| 157 |
+
struct NonLiteral {
|
| 158 |
+
NonLiteral(int n) : n(n) { }
|
| 159 |
+
int n;
|
| 160 |
+
};
|
| 161 |
+
static_assert(ID(NonLiteral{3}).n == 3); // ill-formed
|
| 162 |
+
```
|
| 163 |
+
|
| 164 |
+
— *end example*]
|
| 165 |
+
|
| 166 |
+
[*Example 3*:
|
| 167 |
+
|
| 168 |
+
``` cpp
|
| 169 |
+
auto monoid = [](auto v) { return [=] { return v; }; };
|
| 170 |
+
auto add = [](auto m1) constexpr {
|
| 171 |
+
auto ret = m1();
|
| 172 |
+
return [=](auto m2) mutable {
|
| 173 |
+
auto m1val = m1();
|
| 174 |
+
auto plus = [=](auto m2val) mutable constexpr
|
| 175 |
+
{ return m1val += m2val; };
|
| 176 |
+
ret = plus(m2());
|
| 177 |
+
return monoid(ret);
|
| 178 |
+
};
|
| 179 |
+
};
|
| 180 |
+
constexpr auto zero = monoid(0);
|
| 181 |
+
constexpr auto one = monoid(1);
|
| 182 |
+
static_assert(add(one)(zero)() == one()); // OK
|
| 183 |
+
|
| 184 |
+
// Since two below is not declared constexpr, an evaluation of its constexpr member function call operator
|
| 185 |
+
// cannot perform an lvalue-to-rvalue conversion on one of its subobjects (that represents its capture)
|
| 186 |
+
// in a constant expression.
|
| 187 |
+
auto two = monoid(2);
|
| 188 |
+
assert(two() == 2); // OK, not a constant expression.
|
| 189 |
+
static_assert(add(one)(one)() == two()); // ill-formed: two() is not a constant expression
|
| 190 |
+
static_assert(add(one)(one)() == monoid(2)()); // OK
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
— *end example*]
|
| 194 |
|
| 195 |
The closure type for a non-generic *lambda-expression* with no
|
| 196 |
+
*lambda-capture* has a conversion function to pointer to function with
|
| 197 |
+
C++language linkage ([[dcl.link]]) having the same parameter and return
|
| 198 |
+
types as the closure type’s function call operator. The conversion is to
|
| 199 |
+
“pointer to `noexcept` function” if the function call operator has a
|
| 200 |
+
non-throwing exception specification. The value returned by this
|
| 201 |
+
conversion function is the address of a function `F` that, when invoked,
|
| 202 |
+
has the same effect as invoking the closure type’s function call
|
| 203 |
+
operator. `F` is a constexpr function if the function call operator is a
|
| 204 |
+
constexpr function. For a generic lambda with no *lambda-capture*, the
|
| 205 |
+
closure type has a conversion function template to pointer to function.
|
| 206 |
+
The conversion function template has the same invented
|
| 207 |
*template-parameter-list*, and the pointer to function has the same
|
| 208 |
parameter types, as the function call operator template. The return type
|
| 209 |
of the pointer to function shall behave as if it were a
|
| 210 |
*decltype-specifier* denoting the return type of the corresponding
|
| 211 |
+
function call operator template specialization.
|
| 212 |
+
|
| 213 |
+
[*Note 3*:
|
| 214 |
+
|
| 215 |
+
If the generic lambda has no *trailing-return-type* or the
|
| 216 |
+
*trailing-return-type* contains a placeholder type, return type
|
| 217 |
+
deduction of the corresponding function call operator template
|
| 218 |
+
specialization has to be done. The corresponding specialization is that
|
| 219 |
+
instantiation of the function call operator template with the same
|
| 220 |
+
template arguments as those deduced for the conversion function
|
| 221 |
+
template. Consider the following:
|
| 222 |
|
| 223 |
``` cpp
|
| 224 |
auto glambda = [](auto a) { return a; };
|
| 225 |
int (*fp)(int) = glambda;
|
| 226 |
```
|
|
|
|
| 242 |
template<class T> operator fptr_t<T>() const
|
| 243 |
{ return &lambda_call_operator_invoker; }
|
| 244 |
};
|
| 245 |
```
|
| 246 |
|
| 247 |
+
— *end note*]
|
| 248 |
+
|
| 249 |
+
[*Example 4*:
|
| 250 |
+
|
| 251 |
``` cpp
|
| 252 |
void f1(int (*)(int)) { }
|
| 253 |
void f2(char (*)(int)) { }
|
| 254 |
|
| 255 |
void g(int (*)(int)) { } // #1
|
|
|
|
| 264 |
g(glambda); // error: ambiguous
|
| 265 |
h(glambda); // OK: calls #3 since it is convertible from ID
|
| 266 |
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
| 267 |
```
|
| 268 |
|
| 269 |
+
— *end example*]
|
| 270 |
+
|
| 271 |
The value returned by any given specialization of this conversion
|
| 272 |
+
function template is the address of a function `F` that, when invoked,
|
| 273 |
has the same effect as invoking the generic lambda’s corresponding
|
| 274 |
+
function call operator template specialization. `F` is a constexpr
|
| 275 |
+
function if the corresponding specialization is a constexpr function.
|
| 276 |
+
|
| 277 |
+
[*Note 4*: This will result in the implicit instantiation of the
|
| 278 |
+
generic lambda’s body. The instantiated generic lambda’s return type and
|
| 279 |
+
parameter types shall match the return type and parameter types of the
|
| 280 |
+
pointer to function. — *end note*]
|
| 281 |
+
|
| 282 |
+
[*Example 5*:
|
| 283 |
|
| 284 |
``` cpp
|
| 285 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 286 |
int (*GL_int)(int) = GL; // OK: through conversion function template
|
| 287 |
GL_int(3); // OK: same as GL(3)
|
| 288 |
```
|
| 289 |
|
| 290 |
+
— *end example*]
|
| 291 |
+
|
| 292 |
+
The conversion function or conversion function template is public,
|
| 293 |
+
constexpr, non-virtual, non-explicit, const, and has a non-throwing
|
| 294 |
+
exception specification ([[except.spec]]).
|
| 295 |
+
|
| 296 |
+
[*Example 6*:
|
| 297 |
+
|
| 298 |
+
``` cpp
|
| 299 |
+
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
|
| 300 |
+
auto C = [](auto a) { return a; };
|
| 301 |
+
|
| 302 |
+
static_assert(Fwd(C,3) == 3); // OK
|
| 303 |
+
|
| 304 |
+
// No specialization of the function call operator template can be constexpr (due to the local static).
|
| 305 |
+
auto NC = [](auto a) { static int s; return a; };
|
| 306 |
+
static_assert(Fwd(NC,3) == 3); // ill-formed
|
| 307 |
+
```
|
| 308 |
+
|
| 309 |
+
— *end example*]
|
| 310 |
+
|
| 311 |
The *lambda-expression*’s *compound-statement* yields the
|
| 312 |
*function-body* ([[dcl.fct.def]]) of the function call operator, but
|
| 313 |
for purposes of name lookup ([[basic.lookup]]), determining the type
|
| 314 |
and value of `this` ([[class.this]]) and transforming *id-expression*s
|
| 315 |
referring to non-static class members into class member access
|
| 316 |
expressions using `(*this)` ([[class.mfct.non-static]]), the
|
| 317 |
*compound-statement* is considered in the context of the
|
| 318 |
*lambda-expression*.
|
| 319 |
|
| 320 |
+
[*Example 7*:
|
| 321 |
+
|
| 322 |
``` cpp
|
| 323 |
struct S1 {
|
| 324 |
int x, y;
|
| 325 |
int operator()(int);
|
| 326 |
void f() {
|
|
|
|
| 330 |
};
|
| 331 |
}
|
| 332 |
};
|
| 333 |
```
|
| 334 |
|
| 335 |
+
— *end example*]
|
| 336 |
+
|
| 337 |
Further, a variable `__func__` is implicitly defined at the beginning of
|
| 338 |
the *compound-statement* of the *lambda-expression*, with semantics as
|
| 339 |
described in [[dcl.fct.def.general]].
|
| 340 |
|
| 341 |
+
The closure type associated with a *lambda-expression* has no default
|
| 342 |
+
constructor and a deleted copy assignment operator. It has a defaulted
|
| 343 |
+
copy constructor and a defaulted move constructor ([[class.copy]]).
|
| 344 |
+
|
| 345 |
+
[*Note 5*: These special member functions are implicitly defined as
|
| 346 |
+
usual, and might therefore be defined as deleted. — *end note*]
|
| 347 |
+
|
| 348 |
+
The closure type associated with a *lambda-expression* has an
|
| 349 |
+
implicitly-declared destructor ([[class.dtor]]).
|
| 350 |
+
|
| 351 |
+
A member of a closure type shall not be explicitly instantiated (
|
| 352 |
+
[[temp.explicit]]), explicitly specialized ([[temp.expl.spec]]), or
|
| 353 |
+
named in a `friend` declaration ([[class.friend]]).
|
| 354 |
+
|
| 355 |
+
#### Captures <a id="expr.prim.lambda.capture">[[expr.prim.lambda.capture]]</a>
|
| 356 |
+
|
| 357 |
+
``` bnf
|
| 358 |
+
lambda-capture:
|
| 359 |
+
capture-default
|
| 360 |
+
capture-list
|
| 361 |
+
capture-default ',' capture-list
|
| 362 |
+
```
|
| 363 |
+
|
| 364 |
+
``` bnf
|
| 365 |
+
capture-default:
|
| 366 |
+
'&'
|
| 367 |
+
'='
|
| 368 |
+
```
|
| 369 |
+
|
| 370 |
+
``` bnf
|
| 371 |
+
capture-list:
|
| 372 |
+
capture '...'ₒₚₜ
|
| 373 |
+
capture-list ',' capture '...'ₒₚₜ
|
| 374 |
+
```
|
| 375 |
+
|
| 376 |
+
``` bnf
|
| 377 |
+
capture:
|
| 378 |
+
simple-capture
|
| 379 |
+
init-capture
|
| 380 |
+
```
|
| 381 |
+
|
| 382 |
+
``` bnf
|
| 383 |
+
simple-capture:
|
| 384 |
+
identifier
|
| 385 |
+
'&' identifier
|
| 386 |
+
'this'
|
| 387 |
+
'* this'
|
| 388 |
+
```
|
| 389 |
+
|
| 390 |
+
``` bnf
|
| 391 |
+
init-capture:
|
| 392 |
+
identifier initializer
|
| 393 |
+
'&' identifier initializer
|
| 394 |
+
```
|
| 395 |
+
|
| 396 |
+
The body of a *lambda-expression* may refer to variables with automatic
|
| 397 |
+
storage duration and the `*this` object (if any) of enclosing block
|
| 398 |
+
scopes by capturing those entities, as described below.
|
| 399 |
+
|
| 400 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 401 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 402 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 403 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
| 404 |
+
form “`&` *identifier*” or “`* this`”.
|
| 405 |
+
|
| 406 |
+
[*Note 1*: The form `[&,this]` is redundant but accepted for
|
| 407 |
+
compatibility with ISO C++14. — *end note*]
|
| 408 |
+
|
| 409 |
+
Ignoring appearances in *initializer*s of *init-capture*s, an identifier
|
| 410 |
+
or `this` shall not appear more than once in a *lambda-capture*.
|
| 411 |
+
|
| 412 |
+
[*Example 1*:
|
| 413 |
|
| 414 |
``` cpp
|
| 415 |
struct S2 { void f(int i); };
|
| 416 |
void S2::f(int i) {
|
| 417 |
[&, i]{ }; // OK
|
| 418 |
[&, &i]{ }; // error: i preceded by & when & is the default
|
| 419 |
+
[=, *this]{ }; // OK
|
| 420 |
[=, this]{ }; // error: this when = is the default
|
| 421 |
[i, i]{ }; // error: i repeated
|
| 422 |
+
[this, *this]{ }; // error: this appears twice
|
| 423 |
}
|
| 424 |
```
|
| 425 |
|
| 426 |
+
— *end example*]
|
| 427 |
+
|
| 428 |
A *lambda-expression* whose smallest enclosing scope is a block scope (
|
| 429 |
[[basic.scope.block]]) is a *local lambda expression*; any other
|
| 430 |
*lambda-expression* shall not have a *capture-default* or
|
| 431 |
*simple-capture* in its *lambda-introducer*. The *reaching scope* of a
|
| 432 |
local lambda expression is the set of enclosing scopes up to and
|
| 433 |
+
including the innermost enclosing function and its parameters.
|
| 434 |
+
|
| 435 |
+
[*Note 2*: This reaching scope includes any intervening
|
| 436 |
+
*lambda-expression*s. — *end note*]
|
| 437 |
|
| 438 |
The *identifier* in a *simple-capture* is looked up using the usual
|
| 439 |
rules for unqualified name lookup ([[basic.lookup.unqual]]); each such
|
| 440 |
lookup shall find an entity. An entity that is designated by a
|
| 441 |
*simple-capture* is said to be *explicitly captured*, and shall be
|
| 442 |
+
`*this` (when the *simple-capture* is “`this`” or “`* this`”) or a
|
| 443 |
+
variable with automatic storage duration declared in the reaching scope
|
| 444 |
+
of the local lambda expression.
|
| 445 |
+
|
| 446 |
+
If an *identifier* in a *simple-capture* appears as the *declarator-id*
|
| 447 |
+
of a parameter of the *lambda-declarator*'s
|
| 448 |
+
*parameter-declaration-clause*, the program is ill-formed.
|
| 449 |
+
|
| 450 |
+
[*Example 2*:
|
| 451 |
+
|
| 452 |
+
``` cpp
|
| 453 |
+
void f() {
|
| 454 |
+
int x = 0;
|
| 455 |
+
auto g = [x](int x) { return 0; } // error: parameter and simple-capture have the same name
|
| 456 |
+
}
|
| 457 |
+
```
|
| 458 |
+
|
| 459 |
+
— *end example*]
|
| 460 |
|
| 461 |
An *init-capture* behaves as if it declares and explicitly captures a
|
| 462 |
variable of the form “`auto` *init-capture* `;`” whose declarative
|
| 463 |
region is the *lambda-expression*’s *compound-statement*, except that:
|
| 464 |
|
|
|
|
| 468 |
non-static data member, and no additional copy and destruction is
|
| 469 |
performed, and
|
| 470 |
- if the capture is by reference, the variable’s lifetime ends when the
|
| 471 |
closure object’s lifetime ends.
|
| 472 |
|
| 473 |
+
[*Note 3*: This enables an *init-capture* like “`x = std::move(x)`”;
|
| 474 |
+
the second “`x`” must bind to a declaration in the surrounding
|
| 475 |
+
context. — *end note*]
|
| 476 |
+
|
| 477 |
+
[*Example 3*:
|
| 478 |
|
| 479 |
``` cpp
|
| 480 |
int x = 4;
|
| 481 |
auto y = [&r = x, x = x+1]()->int {
|
| 482 |
r += 2;
|
| 483 |
return x+2;
|
| 484 |
}(); // Updates ::x to 6, and initializes y to 7.
|
| 485 |
+
|
| 486 |
+
auto z = [a = 42](int a) { return 1; } // error: parameter and local variable have the same name
|
| 487 |
```
|
| 488 |
|
| 489 |
+
— *end example*]
|
| 490 |
+
|
| 491 |
A *lambda-expression* with an associated *capture-default* that does not
|
| 492 |
+
explicitly capture `*this` or a variable with automatic storage duration
|
| 493 |
(this excludes any *id-expression* that has been found to refer to an
|
| 494 |
*init-capture*'s associated non-static data member), is said to
|
| 495 |
+
*implicitly capture* the entity (i.e., `*this` or a variable) if the
|
| 496 |
*compound-statement*:
|
| 497 |
|
| 498 |
+
- odr-uses ([[basic.def.odr]]) the entity (in the case of a variable),
|
| 499 |
+
- odr-uses ([[basic.def.odr]]) `this` (in the case of the object
|
| 500 |
+
designated by `*this`), or
|
| 501 |
- names the entity in a potentially-evaluated expression (
|
| 502 |
[[basic.def.odr]]) where the enclosing full-expression depends on a
|
| 503 |
generic lambda parameter declared within the reaching scope of the
|
| 504 |
*lambda-expression*.
|
| 505 |
|
| 506 |
+
[*Example 4*:
|
| 507 |
+
|
| 508 |
``` cpp
|
| 509 |
void f(int, const int (&)[2] = {}) { } // #1
|
| 510 |
void f(const int&, const int (&)[1]) { } // #2
|
| 511 |
void test() {
|
| 512 |
const int x = 17;
|
|
|
|
| 519 |
f(x, selector); // OK: is a dependent expression, so captures x
|
| 520 |
};
|
| 521 |
}
|
| 522 |
```
|
| 523 |
|
| 524 |
+
— *end example*]
|
| 525 |
+
|
| 526 |
All such implicitly captured entities shall be declared within the
|
| 527 |
+
reaching scope of the lambda expression.
|
| 528 |
+
|
| 529 |
+
[*Note 4*: The implicit capture of an entity by a nested
|
| 530 |
+
*lambda-expression* can cause its implicit capture by the containing
|
| 531 |
+
*lambda-expression* (see below). Implicit odr-uses of `this` can result
|
| 532 |
+
in implicit capture. — *end note*]
|
| 533 |
|
| 534 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 535 |
entity captured by a *lambda-expression* is odr-used (
|
| 536 |
[[basic.def.odr]]) in the scope containing the *lambda-expression*. If
|
| 537 |
+
`*this` is captured by a local lambda expression, its nearest enclosing
|
| 538 |
function shall be a non-static member function. If a *lambda-expression*
|
| 539 |
or an instantiation of the function call operator template of a generic
|
| 540 |
lambda odr-uses ([[basic.def.odr]]) `this` or a variable with automatic
|
| 541 |
storage duration from its reaching scope, that entity shall be captured
|
| 542 |
by the *lambda-expression*. If a *lambda-expression* captures an entity
|
| 543 |
and that entity is not defined or captured in the immediately enclosing
|
| 544 |
lambda expression or function, the program is ill-formed.
|
| 545 |
|
| 546 |
+
[*Example 5*:
|
| 547 |
+
|
| 548 |
``` cpp
|
| 549 |
void f1(int i) {
|
| 550 |
int const N = 20;
|
| 551 |
auto m1 = [=]{
|
| 552 |
int const M = 30;
|
| 553 |
auto m2 = [i]{
|
| 554 |
int x[N][M]; // OK: N and M are not odr-used
|
| 555 |
+
x[0][0] = i; // OK: i is explicitly captured by m2 and implicitly captured by m1
|
|
|
|
| 556 |
};
|
| 557 |
};
|
| 558 |
struct s1 {
|
| 559 |
int f;
|
| 560 |
void work(int n) {
|
| 561 |
int m = n*n;
|
| 562 |
int j = 40;
|
| 563 |
auto m3 = [this,m] {
|
| 564 |
auto m4 = [&,j] { // error: j not captured by m3
|
| 565 |
+
int x = n; // error: n implicitly captured by m4 but not captured by m3
|
| 566 |
+
x += m; // OK: m implicitly captured by m4 and explicitly captured by m3
|
|
|
|
|
|
|
| 567 |
x += i; // error: i is outside of the reaching scope
|
| 568 |
+
x += f; // OK: this captured implicitly by m4 and explicitly by m3
|
|
|
|
| 569 |
};
|
| 570 |
};
|
| 571 |
}
|
| 572 |
};
|
| 573 |
}
|
| 574 |
+
|
| 575 |
+
struct s2 {
|
| 576 |
+
double ohseven = .007;
|
| 577 |
+
auto f() {
|
| 578 |
+
return [this] {
|
| 579 |
+
return [*this] {
|
| 580 |
+
return ohseven; // OK
|
| 581 |
+
}
|
| 582 |
+
}();
|
| 583 |
+
}
|
| 584 |
+
auto g() {
|
| 585 |
+
return [] {
|
| 586 |
+
return [*this] { }; // error: *this not captured by outer lambda-expression
|
| 587 |
+
}();
|
| 588 |
+
}
|
| 589 |
+
};
|
| 590 |
```
|
| 591 |
|
| 592 |
+
— *end example*]
|
| 593 |
+
|
| 594 |
A *lambda-expression* appearing in a default argument shall not
|
| 595 |
implicitly or explicitly capture any entity.
|
| 596 |
|
| 597 |
+
[*Example 6*:
|
| 598 |
+
|
| 599 |
``` cpp
|
| 600 |
void f2() {
|
| 601 |
int i = 1;
|
| 602 |
void g1(int = ([i]{ return i; })()); // ill-formed
|
| 603 |
void g2(int = ([i]{ return 0; })()); // ill-formed
|
|
|
|
| 605 |
void g4(int = ([=]{ return 0; })()); // OK
|
| 606 |
void g5(int = ([]{ return sizeof i; })()); // OK
|
| 607 |
}
|
| 608 |
```
|
| 609 |
|
| 610 |
+
— *end example*]
|
| 611 |
+
|
| 612 |
+
An entity is *captured by copy* if
|
| 613 |
+
|
| 614 |
+
- it is implicitly captured, the *capture-default* is `=`, and the
|
| 615 |
+
captured entity is not `*this`, or
|
| 616 |
+
- it is explicitly captured with a capture that is not of the form
|
| 617 |
+
`this`, `&` *identifier*, or `&` *identifier* *initializer*.
|
| 618 |
+
|
| 619 |
+
For each entity captured by copy, an unnamed non-static data member is
|
| 620 |
+
declared in the closure type. The declaration order of these members is
|
| 621 |
+
unspecified. The type of such a data member is the referenced type if
|
| 622 |
+
the entity is a reference to an object, an lvalue reference to the
|
| 623 |
+
referenced function type if the entity is a reference to a function, or
|
| 624 |
+
the type of the corresponding captured entity otherwise. A member of an
|
| 625 |
+
anonymous union shall not be captured by copy.
|
| 626 |
+
|
| 627 |
+
Every *id-expression* within the *compound-statement* of a
|
| 628 |
+
*lambda-expression* that is an odr-use ([[basic.def.odr]]) of an entity
|
| 629 |
+
captured by copy is transformed into an access to the corresponding
|
| 630 |
+
unnamed data member of the closure type.
|
| 631 |
+
|
| 632 |
+
[*Note 5*: An *id-expression* that is not an odr-use refers to the
|
| 633 |
+
original entity, never to a member of the closure type. Furthermore,
|
| 634 |
+
such an *id-expression* does not cause the implicit capture of the
|
| 635 |
+
entity. — *end note*]
|
| 636 |
+
|
| 637 |
+
If `*this` is captured by copy, each odr-use of `this` is transformed
|
| 638 |
+
into a pointer to the corresponding unnamed data member of the closure
|
| 639 |
+
type, cast ([[expr.cast]]) to the type of `this`.
|
| 640 |
+
|
| 641 |
+
[*Note 6*: The cast ensures that the transformed expression is a
|
| 642 |
+
prvalue. — *end note*]
|
| 643 |
+
|
| 644 |
+
An *id-expression* within the *compound-statement* of a
|
| 645 |
+
*lambda-expression* that is an odr-use of a reference captured by
|
| 646 |
+
reference refers to the entity to which the captured reference is bound
|
| 647 |
+
and not to the captured reference.
|
| 648 |
+
|
| 649 |
+
[*Note 7*: The validity of such captures is determined by the lifetime
|
| 650 |
+
of the object to which the reference refers, not by the lifetime of the
|
| 651 |
+
reference itself. — *end note*]
|
| 652 |
+
|
| 653 |
+
[*Example 7*:
|
| 654 |
+
|
| 655 |
+
``` cpp
|
| 656 |
+
void f(const int*);
|
| 657 |
+
void g() {
|
| 658 |
+
const int N = 10;
|
| 659 |
+
[=] {
|
| 660 |
+
int arr[N]; // OK: not an odr-use, refers to automatic variable
|
| 661 |
+
f(&N); // OK: causes N to be captured; &N points to
|
| 662 |
+
// the corresponding member of the closure type
|
| 663 |
+
};
|
| 664 |
+
}
|
| 665 |
+
auto h(int &r) {
|
| 666 |
+
return [&] {
|
| 667 |
+
++r; // Valid after h returns if the lifetime of the
|
| 668 |
+
// object to which r is bound has not ended
|
| 669 |
+
};
|
| 670 |
+
}
|
| 671 |
+
```
|
| 672 |
+
|
| 673 |
+
— *end example*]
|
| 674 |
|
| 675 |
An entity is *captured by reference* if it is implicitly or explicitly
|
| 676 |
captured but not captured by copy. It is unspecified whether additional
|
| 677 |
unnamed non-static data members are declared in the closure type for
|
| 678 |
+
entities captured by reference. If declared, such non-static data
|
| 679 |
+
members shall be of literal type.
|
| 680 |
+
|
| 681 |
+
[*Example 8*:
|
| 682 |
+
|
| 683 |
+
``` cpp
|
| 684 |
+
// The inner closure type must be a literal type regardless of how reference captures are represented.
|
| 685 |
+
static_assert([](int n) { return [&n] { return ++n; }(); }(3) == 4);
|
| 686 |
+
```
|
| 687 |
+
|
| 688 |
+
— *end example*]
|
| 689 |
+
|
| 690 |
+
A bit-field or a member of an anonymous union shall not be captured by
|
| 691 |
+
reference.
|
| 692 |
|
| 693 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 694 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 695 |
`m2`’s capture is transformed as follows:
|
| 696 |
|
| 697 |
- if `m1` captures the entity by copy, `m2` captures the corresponding
|
| 698 |
non-static data member of `m1`’s closure type;
|
| 699 |
- if `m1` captures the entity by reference, `m2` captures the same
|
| 700 |
entity captured by `m1`.
|
| 701 |
|
| 702 |
+
[*Example 9*:
|
| 703 |
+
|
| 704 |
+
The nested lambda expressions and invocations below will output
|
| 705 |
`123234`.
|
| 706 |
|
| 707 |
``` cpp
|
| 708 |
int a = 1, b = 1, c = 1;
|
| 709 |
auto m1 = [a, &b, &c]() mutable {
|
|
|
|
| 717 |
a = 2; b = 2; c = 2;
|
| 718 |
m1();
|
| 719 |
std::cout << a << b << c;
|
| 720 |
```
|
| 721 |
|
| 722 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 723 |
|
| 724 |
Every occurrence of `decltype((x))` where `x` is a possibly
|
| 725 |
parenthesized *id-expression* that names an entity of automatic storage
|
| 726 |
duration is treated as if `x` were transformed into an access to a
|
| 727 |
corresponding data member of the closure type that would have been
|
| 728 |
declared if `x` were an odr-use of the denoted entity.
|
| 729 |
|
| 730 |
+
[*Example 10*:
|
| 731 |
+
|
| 732 |
``` cpp
|
| 733 |
void f3() {
|
| 734 |
float x, &r = x;
|
| 735 |
[=] { // x and r are not captured (appearance in a decltype operand is not an odr-use)
|
| 736 |
decltype(x) y1; // y1 has type float
|
| 737 |
+
decltype((x)) y2 = y1; // y2 has type float const& because this lambda is not mutable and x is an lvalue
|
|
|
|
| 738 |
decltype(r) r1 = y1; // r1 has type float& (transformation not considered)
|
| 739 |
decltype((r)) r2 = y2; // r2 has type float const&
|
| 740 |
};
|
| 741 |
}
|
| 742 |
```
|
| 743 |
|
| 744 |
+
— *end example*]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
|
| 746 |
When the *lambda-expression* is evaluated, the entities that are
|
| 747 |
captured by copy are used to direct-initialize each corresponding
|
| 748 |
non-static data member of the resulting closure object, and the
|
| 749 |
non-static data members corresponding to the *init-capture*s are
|
| 750 |
initialized as indicated by the corresponding *initializer* (which may
|
| 751 |
be copy- or direct-initialization). (For array members, the array
|
| 752 |
elements are direct-initialized in increasing subscript order.) These
|
| 753 |
initializations are performed in the (unspecified) order in which the
|
| 754 |
+
non-static data members are declared.
|
|
|
|
| 755 |
|
| 756 |
+
[*Note 8*: This ensures that the destructions will occur in the reverse
|
| 757 |
+
order of the constructions. — *end note*]
|
| 758 |
+
|
| 759 |
+
[*Note 9*: If a non-reference entity is implicitly or explicitly
|
| 760 |
+
captured by reference, invoking the function call operator of the
|
| 761 |
+
corresponding *lambda-expression* after the lifetime of the entity has
|
| 762 |
+
ended is likely to result in undefined behavior. — *end note*]
|
| 763 |
|
| 764 |
A *simple-capture* followed by an ellipsis is a pack expansion (
|
| 765 |
[[temp.variadic]]). An *init-capture* followed by an ellipsis is
|
| 766 |
ill-formed.
|
| 767 |
|
| 768 |
+
[*Example 11*:
|
| 769 |
+
|
| 770 |
``` cpp
|
| 771 |
template<class... Args>
|
| 772 |
void f(Args... args) {
|
| 773 |
auto lm = [&, args...] { return g(args...); };
|
| 774 |
lm();
|
| 775 |
}
|
| 776 |
```
|
| 777 |
|
| 778 |
+
— *end example*]
|
| 779 |
+
|