- tmp/tmp5opp3a39/{from.md → to.md} +280 -118
tmp/tmp5opp3a39/{from.md → to.md}
RENAMED
|
@@ -105,42 +105,40 @@ The result is an lvalue if the entity is a function, variable, or data
|
|
| 105 |
member and a prvalue otherwise.
|
| 106 |
|
| 107 |
``` bnf
|
| 108 |
qualified-id:
|
| 109 |
nested-name-specifier 'template'ₒₚₜ unqualified-id
|
| 110 |
-
'::' identifier
|
| 111 |
-
'::' operator-function-id
|
| 112 |
-
'::' literal-operator-id
|
| 113 |
-
'::' template-id
|
| 114 |
```
|
| 115 |
|
| 116 |
``` bnf
|
| 117 |
nested-name-specifier:
|
| 118 |
-
'::'
|
| 119 |
-
|
|
|
|
| 120 |
decltype-specifier '::'
|
| 121 |
nested-name-specifier identifier '::'
|
| 122 |
nested-name-specifier 'template'ₒₚₜ simple-template-id '::'
|
| 123 |
```
|
| 124 |
|
|
|
|
|
|
|
|
|
|
| 125 |
A *nested-name-specifier* that denotes a class, optionally followed by
|
| 126 |
the keyword `template` ([[temp.names]]), and then followed by the name
|
| 127 |
of a member of either that class ([[class.mem]]) or one of its base
|
| 128 |
classes (Clause [[class.derived]]), is a *qualified-id*;
|
| 129 |
[[class.qual]] describes name lookup for class members that appear in
|
| 130 |
*qualified-ids*. The result is the member. The type of the result is the
|
| 131 |
type of the member. The result is an lvalue if the member is a static
|
| 132 |
member function or a data member and a prvalue otherwise. a class member
|
| 133 |
can be referred to using a *qualified-id* at any point in its potential
|
| 134 |
-
scope ([[basic.scope.class]]). Where *class-name* `::`
|
| 135 |
-
used,
|
| 136 |
-
names the
|
| 137 |
-
*
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
it shall not be used as the *unqualified-id* in a *qualified-id*. a
|
| 141 |
-
*typedef-name* that names a class is a *class-name* ([[class.name]]).
|
| 142 |
|
| 143 |
A `::`, or a *nested-name-specifier* that names a namespace (
|
| 144 |
[[basic.namespace]]), in either case followed by the name of a member of
|
| 145 |
that namespace (or the name of a member of a namespace made visible by a
|
| 146 |
*using-directive*) is a *qualified-id*; [[namespace.qual]] describes
|
|
@@ -165,14 +163,10 @@ member function of a class can only be used:
|
|
| 165 |
|
| 166 |
- as part of a class member access ([[expr.ref]]) in which the object
|
| 167 |
expression refers to the member’s class[^4] or a class derived from
|
| 168 |
that class, or
|
| 169 |
- to form a pointer to member ([[expr.unary.op]]), or
|
| 170 |
-
- in a *mem-initializer* for a constructor for that class or for a class
|
| 171 |
-
derived from that class ([[class.base.init]]), or
|
| 172 |
-
- in a *brace-or-equal-initializer* for a non-static data member of that
|
| 173 |
-
class or of a class derived from that class ([[class.base.init]]), or
|
| 174 |
- if that *id-expression* denotes a non-static data member and it
|
| 175 |
appears in an unevaluated operand.
|
| 176 |
``` cpp
|
| 177 |
struct S {
|
| 178 |
int m;
|
|
@@ -226,39 +220,54 @@ capture-list:
|
|
| 226 |
capture-list ',' capture '...'ₒₚₜ
|
| 227 |
```
|
| 228 |
|
| 229 |
``` bnf
|
| 230 |
capture:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
identifier
|
| 232 |
'&' identifier
|
| 233 |
'this'
|
| 234 |
```
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
``` bnf
|
| 237 |
lambda-declarator:
|
| 238 |
'(' parameter-declaration-clause ')' 'mutable'ₒₚₜ
|
| 239 |
exception-specificationₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 240 |
```
|
| 241 |
|
| 242 |
The evaluation of a *lambda-expression* results in a prvalue temporary (
|
| 243 |
[[class.temporary]]). This temporary is called the *closure object*. A
|
| 244 |
*lambda-expression* shall not appear in an unevaluated operand (Clause
|
| 245 |
-
[[expr]])
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
| 247 |
|
| 248 |
The type of the *lambda-expression* (which is also the type of the
|
| 249 |
closure object) is a unique, unnamed non-union class type — called the
|
| 250 |
*closure type* — whose properties are described below. This class type
|
| 251 |
-
is
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
[[basic.lookup.argdep]]). The
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
other than by changing:
|
| 260 |
|
| 261 |
- the size and/or alignment of the closure type,
|
| 262 |
- whether the closure type is trivially copyable (Clause [[class]]),
|
| 263 |
- whether the closure type is a standard-layout class (Clause
|
| 264 |
[[class]]), or
|
|
@@ -266,52 +275,145 @@ other than by changing:
|
|
| 266 |
|
| 267 |
An implementation shall not add members of rvalue reference type to the
|
| 268 |
closure type.
|
| 269 |
|
| 270 |
If a *lambda-expression* does not include a *lambda-declarator*, it is
|
| 271 |
-
as if the *lambda-declarator* were `()`.
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
- if the *compound-statement* is of the form
|
| 276 |
-
``` bnf
|
| 277 |
-
'{' attribute-specifier-seqₒₚₜ 'return' expression ';' '}'
|
| 278 |
-
```
|
| 279 |
-
|
| 280 |
-
the type of the returned expression after lvalue-to-rvalue
|
| 281 |
-
conversion ([[conv.lval]]), array-to-pointer conversion (
|
| 282 |
-
[[conv.array]]), and function-to-pointer conversion ([[conv.func]]);
|
| 283 |
-
- otherwise, `void`.
|
| 284 |
|
| 285 |
``` cpp
|
| 286 |
auto x1 = [](int i){ return i; }; // OK: return type is int
|
| 287 |
-
auto x2 = []{ return { 1, 2 }; };
|
| 288 |
-
|
|
|
|
| 289 |
```
|
| 290 |
|
| 291 |
-
The closure type for a *lambda-expression* has a public
|
| 292 |
-
function call operator ([[over.call]]) whose parameters and
|
| 293 |
-
are described by the *lambda-expression*’s
|
| 294 |
*parameter-declaration-clause* and *trailing-return-type* respectively.
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
[[class.mfct.non-static]]) if and only if the *lambda-expression*’s
|
| 297 |
*parameter-declaration-clause* is not followed by `mutable`. It is
|
| 298 |
-
neither virtual nor declared `volatile`.
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
*
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
The *lambda-expression*’s *compound-statement* yields the
|
| 315 |
*function-body* ([[dcl.fct.def]]) of the function call operator, but
|
| 316 |
for purposes of name lookup ([[basic.lookup]]), determining the type
|
| 317 |
and value of `this` ([[class.this]]) and transforming *id-expression*s
|
|
@@ -331,16 +433,21 @@ struct S1 {
|
|
| 331 |
};
|
| 332 |
}
|
| 333 |
};
|
| 334 |
```
|
| 335 |
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
``` cpp
|
| 344 |
struct S2 { void f(int i); };
|
| 345 |
void S2::f(int i) {
|
| 346 |
[&, i]{ }; // OK
|
|
@@ -349,40 +456,89 @@ void S2::f(int i) {
|
|
| 349 |
[i, i]{ }; // error: i repeated
|
| 350 |
}
|
| 351 |
```
|
| 352 |
|
| 353 |
A *lambda-expression* whose smallest enclosing scope is a block scope (
|
| 354 |
-
[[basic.scope.
|
| 355 |
-
*lambda-expression* shall not have a *capture-
|
| 356 |
-
*lambda-introducer*. The *reaching scope* of a
|
| 357 |
-
is the set of enclosing scopes up to and
|
| 358 |
-
enclosing function and its parameters. This
|
| 359 |
-
intervening *lambda-expression*s.
|
| 360 |
|
| 361 |
-
The *
|
| 362 |
rules for unqualified name lookup ([[basic.lookup.unqual]]); each such
|
| 363 |
-
lookup shall find
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
the
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
*
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 379 |
entity captured by a *lambda-expression* is odr-used (
|
| 380 |
[[basic.def.odr]]) in the scope containing the *lambda-expression*. If
|
| 381 |
`this` is captured by a local lambda expression, its nearest enclosing
|
| 382 |
function shall be a non-static member function. If a *lambda-expression*
|
| 383 |
-
|
|
|
|
| 384 |
storage duration from its reaching scope, that entity shall be captured
|
| 385 |
by the *lambda-expression*. If a *lambda-expression* captures an entity
|
| 386 |
and that entity is not defined or captured in the immediately enclosing
|
| 387 |
lambda expression or function, the program is ill-formed.
|
| 388 |
|
|
@@ -432,22 +588,25 @@ void f2() {
|
|
| 432 |
}
|
| 433 |
```
|
| 434 |
|
| 435 |
An entity is *captured by copy* if it is implicitly captured and the
|
| 436 |
*capture-default* is `=` or if it is explicitly captured with a capture
|
| 437 |
-
that
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
otherwise. If the captured entity is a
|
| 443 |
-
corresponding data member is also a
|
|
|
|
|
|
|
| 444 |
|
| 445 |
An entity is *captured by reference* if it is implicitly or explicitly
|
| 446 |
captured but not captured by copy. It is unspecified whether additional
|
| 447 |
unnamed non-static data members are declared in the closure type for
|
| 448 |
-
entities captured by reference.
|
|
|
|
| 449 |
|
| 450 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 451 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 452 |
`m2`’s capture is transformed as follows:
|
| 453 |
|
|
@@ -472,30 +631,30 @@ auto m1 = [a, &b, &c]() mutable {
|
|
| 472 |
a = 2; b = 2; c = 2;
|
| 473 |
m1();
|
| 474 |
std::cout << a << b << c;
|
| 475 |
```
|
| 476 |
|
| 477 |
-
Every *id-expression*
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
*id-expression* that is not
|
| 481 |
-
|
| 482 |
-
*id-expression* does not cause the
|
| 483 |
-
`this` is captured, each odr-use of
|
| 484 |
-
|
| 485 |
-
[[expr.cast]]) to the type of `this`.
|
| 486 |
-
transformed expression is a prvalue.
|
| 487 |
|
| 488 |
``` cpp
|
| 489 |
void f(const int*);
|
| 490 |
void g() {
|
| 491 |
const int N = 10;
|
| 492 |
[=] {
|
| 493 |
int arr[N]; // OK: not an odr-use, refers to automatic variable
|
| 494 |
f(&N); // OK: causes N to be captured; &N points to the
|
| 495 |
// corresponding member of the closure type
|
| 496 |
-
}
|
| 497 |
}
|
| 498 |
```
|
| 499 |
|
| 500 |
Every occurrence of `decltype((x))` where `x` is a possibly
|
| 501 |
parenthesized *id-expression* that names an entity of automatic storage
|
|
@@ -527,24 +686,27 @@ implicitly defined.
|
|
| 527 |
The closure type associated with a *lambda-expression* has an
|
| 528 |
implicitly-declared destructor ([[class.dtor]]).
|
| 529 |
|
| 530 |
When the *lambda-expression* is evaluated, the entities that are
|
| 531 |
captured by copy are used to direct-initialize each corresponding
|
| 532 |
-
non-static data member of the resulting closure object
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
the
|
|
|
|
|
|
|
| 538 |
|
| 539 |
If an entity is implicitly or explicitly captured by reference, invoking
|
| 540 |
the function call operator of the corresponding *lambda-expression*
|
| 541 |
after the lifetime of the entity has ended is likely to result in
|
| 542 |
undefined behavior.
|
| 543 |
|
| 544 |
-
A *capture* followed by an ellipsis is a pack expansion (
|
| 545 |
-
[[temp.variadic]]).
|
|
|
|
| 546 |
|
| 547 |
``` cpp
|
| 548 |
template<class... Args>
|
| 549 |
void f(Args... args) {
|
| 550 |
auto lm = [&, args...] { return g(args...); };
|
|
|
|
| 105 |
member and a prvalue otherwise.
|
| 106 |
|
| 107 |
``` bnf
|
| 108 |
qualified-id:
|
| 109 |
nested-name-specifier 'template'ₒₚₜ unqualified-id
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
```
|
| 111 |
|
| 112 |
``` bnf
|
| 113 |
nested-name-specifier:
|
| 114 |
+
'::'
|
| 115 |
+
type-name '::'
|
| 116 |
+
namespace-name '::'
|
| 117 |
decltype-specifier '::'
|
| 118 |
nested-name-specifier identifier '::'
|
| 119 |
nested-name-specifier 'template'ₒₚₜ simple-template-id '::'
|
| 120 |
```
|
| 121 |
|
| 122 |
+
The type denoted by a *decltype-specifier* in a *nested-name-specifier*
|
| 123 |
+
shall be a class or enumeration type.
|
| 124 |
+
|
| 125 |
A *nested-name-specifier* that denotes a class, optionally followed by
|
| 126 |
the keyword `template` ([[temp.names]]), and then followed by the name
|
| 127 |
of a member of either that class ([[class.mem]]) or one of its base
|
| 128 |
classes (Clause [[class.derived]]), is a *qualified-id*;
|
| 129 |
[[class.qual]] describes name lookup for class members that appear in
|
| 130 |
*qualified-ids*. The result is the member. The type of the result is the
|
| 131 |
type of the member. The result is an lvalue if the member is a static
|
| 132 |
member function or a data member and a prvalue otherwise. a class member
|
| 133 |
can be referred to using a *qualified-id* at any point in its potential
|
| 134 |
+
scope ([[basic.scope.class]]). Where *class-name* `::~` *class-name* is
|
| 135 |
+
used, the two *class-name*s shall refer to the same class; this notation
|
| 136 |
+
names the destructor ([[class.dtor]]). The form
|
| 137 |
+
`~` *decltype-specifier* also denotes the destructor, but it shall not
|
| 138 |
+
be used as the *unqualified-id* in a *qualified-id*. a *typedef-name*
|
| 139 |
+
that names a class is a *class-name* ([[class.name]]).
|
|
|
|
|
|
|
| 140 |
|
| 141 |
A `::`, or a *nested-name-specifier* that names a namespace (
|
| 142 |
[[basic.namespace]]), in either case followed by the name of a member of
|
| 143 |
that namespace (or the name of a member of a namespace made visible by a
|
| 144 |
*using-directive*) is a *qualified-id*; [[namespace.qual]] describes
|
|
|
|
| 163 |
|
| 164 |
- as part of a class member access ([[expr.ref]]) in which the object
|
| 165 |
expression refers to the member’s class[^4] or a class derived from
|
| 166 |
that class, or
|
| 167 |
- to form a pointer to member ([[expr.unary.op]]), or
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
- if that *id-expression* denotes a non-static data member and it
|
| 169 |
appears in an unevaluated operand.
|
| 170 |
``` cpp
|
| 171 |
struct S {
|
| 172 |
int m;
|
|
|
|
| 220 |
capture-list ',' capture '...'ₒₚₜ
|
| 221 |
```
|
| 222 |
|
| 223 |
``` bnf
|
| 224 |
capture:
|
| 225 |
+
simple-capture
|
| 226 |
+
init-capture
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
``` bnf
|
| 230 |
+
simple-capture:
|
| 231 |
identifier
|
| 232 |
'&' identifier
|
| 233 |
'this'
|
| 234 |
```
|
| 235 |
|
| 236 |
+
``` bnf
|
| 237 |
+
init-capture:
|
| 238 |
+
identifier initializer
|
| 239 |
+
'&' identifier initializer
|
| 240 |
+
```
|
| 241 |
+
|
| 242 |
``` bnf
|
| 243 |
lambda-declarator:
|
| 244 |
'(' parameter-declaration-clause ')' 'mutable'ₒₚₜ
|
| 245 |
exception-specificationₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 246 |
```
|
| 247 |
|
| 248 |
The evaluation of a *lambda-expression* results in a prvalue temporary (
|
| 249 |
[[class.temporary]]). This temporary is called the *closure object*. A
|
| 250 |
*lambda-expression* shall not appear in an unevaluated operand (Clause
|
| 251 |
+
[[expr]]), in a *template-argument*, in an *alias-declaration*, in a
|
| 252 |
+
typedef declaration, or in the declaration of a function or function
|
| 253 |
+
template outside its function body and default arguments. The intention
|
| 254 |
+
is to prevent lambdas from appearing in a signature. A closure object
|
| 255 |
+
behaves like a function object ([[function.objects]]).
|
| 256 |
|
| 257 |
The type of the *lambda-expression* (which is also the type of the
|
| 258 |
closure object) is a unique, unnamed non-union class type — called the
|
| 259 |
*closure type* — whose properties are described below. This class type
|
| 260 |
+
is neither an aggregate ([[dcl.init.aggr]]) nor a literal type (
|
| 261 |
+
[[basic.types]]). The closure type is declared in the smallest block
|
| 262 |
+
scope, class scope, or namespace scope that contains the corresponding
|
| 263 |
+
*lambda-expression*. This determines the set of namespaces and classes
|
| 264 |
+
associated with the closure type ([[basic.lookup.argdep]]). The
|
| 265 |
+
parameter types of a *lambda-declarator* do not affect these associated
|
| 266 |
+
namespaces and classes. An implementation may define the closure type
|
| 267 |
+
differently from what is described below provided this does not alter
|
| 268 |
+
the observable behavior of the program other than by changing:
|
| 269 |
|
| 270 |
- the size and/or alignment of the closure type,
|
| 271 |
- whether the closure type is trivially copyable (Clause [[class]]),
|
| 272 |
- whether the closure type is a standard-layout class (Clause
|
| 273 |
[[class]]), or
|
|
|
|
| 275 |
|
| 276 |
An implementation shall not add members of rvalue reference type to the
|
| 277 |
closure type.
|
| 278 |
|
| 279 |
If a *lambda-expression* does not include a *lambda-declarator*, it is
|
| 280 |
+
as if the *lambda-declarator* were `()`. The lambda return type is
|
| 281 |
+
`auto`, which is replaced by the *trailing-return-type* if provided
|
| 282 |
+
and/or deduced from `return` statements as described in
|
| 283 |
+
[[dcl.spec.auto]].
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
``` cpp
|
| 286 |
auto x1 = [](int i){ return i; }; // OK: return type is int
|
| 287 |
+
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 288 |
+
int j;
|
| 289 |
+
auto x3 = []()->auto&& { return j; }; // OK: return type is int&
|
| 290 |
```
|
| 291 |
|
| 292 |
+
The closure type for a non-generic *lambda-expression* has a public
|
| 293 |
+
inline function call operator ([[over.call]]) whose parameters and
|
| 294 |
+
return type are described by the *lambda-expression*’s
|
| 295 |
*parameter-declaration-clause* and *trailing-return-type* respectively.
|
| 296 |
+
For a generic lambda, the closure type has a public inline function call
|
| 297 |
+
operator member template ([[temp.mem]]) whose *template-parameter-list*
|
| 298 |
+
consists of one invented type *template-parameter* for each occurrence
|
| 299 |
+
of `auto` in the lambda’s *parameter-declaration-clause*, in order of
|
| 300 |
+
appearance. The invented type *template-parameter* is a parameter pack
|
| 301 |
+
if the corresponding *parameter-declaration* declares a function
|
| 302 |
+
parameter pack ([[dcl.fct]]). The return type and function parameters
|
| 303 |
+
of the function call operator template are derived from the
|
| 304 |
+
*lambda-expression*'s *trailing-return-type* and
|
| 305 |
+
*parameter-declaration-clause* by replacing each occurrence of `auto` in
|
| 306 |
+
the *decl-specifier*s of the *parameter-declaration-clause* with the
|
| 307 |
+
name of the corresponding invented *template-parameter*.
|
| 308 |
+
|
| 309 |
+
``` cpp
|
| 310 |
+
auto glambda = [](auto a, auto&& b) { return a < b; };
|
| 311 |
+
bool b = glambda(3, 3.14); // OK
|
| 312 |
+
auto vglambda = [](auto printer) {
|
| 313 |
+
return [=](auto&& ... ts) { // OK: ts is a function parameter pack
|
| 314 |
+
printer(std::forward<decltype(ts)>(ts)...);
|
| 315 |
+
|
| 316 |
+
return [=]() {
|
| 317 |
+
printer(ts ...);
|
| 318 |
+
};
|
| 319 |
+
};
|
| 320 |
+
};
|
| 321 |
+
auto p = vglambda( [](auto v1, auto v2, auto v3)
|
| 322 |
+
{ std::cout << v1 << v2 << v3; } );
|
| 323 |
+
auto q = p(1, 'a', 3.14); // OK: outputs 1a3.14
|
| 324 |
+
q(); // OK: outputs 1a3.14
|
| 325 |
+
```
|
| 326 |
+
|
| 327 |
+
This function call operator or operator template is declared `const` (
|
| 328 |
[[class.mfct.non-static]]) if and only if the *lambda-expression*’s
|
| 329 |
*parameter-declaration-clause* is not followed by `mutable`. It is
|
| 330 |
+
neither virtual nor declared `volatile`. Any *exception-specification*
|
| 331 |
+
specified on a *lambda-expression* applies to the corresponding function
|
| 332 |
+
call operator or operator template. An *attribute-specifier-seq* in a
|
| 333 |
+
*lambda-declarator* appertains to the type of the corresponding function
|
| 334 |
+
call operator or operator template. Names referenced in the
|
| 335 |
+
*lambda-declarator* are looked up in the context in which the
|
| 336 |
+
*lambda-expression* appears.
|
| 337 |
+
|
| 338 |
+
The closure type for a non-generic *lambda-expression* with no
|
| 339 |
+
*lambda-capture* has a public non-virtual non-explicit const conversion
|
| 340 |
+
function to pointer to function with C++language linkage ([[dcl.link]])
|
| 341 |
+
having the same parameter and return types as the closure type’s
|
| 342 |
+
function call operator. The value returned by this conversion function
|
| 343 |
+
shall be the address of a function that, when invoked, has the same
|
| 344 |
+
effect as invoking the closure type’s function call operator. For a
|
| 345 |
+
generic lambda with no *lambda-capture*, the closure type has a public
|
| 346 |
+
non-virtual non-explicit const conversion function template to pointer
|
| 347 |
+
to function. The conversion function template has the same invented
|
| 348 |
+
*template-parameter-list*, and the pointer to function has the same
|
| 349 |
+
parameter types, as the function call operator template. The return type
|
| 350 |
+
of the pointer to function shall behave as if it were a
|
| 351 |
+
*decltype-specifier* denoting the return type of the corresponding
|
| 352 |
+
function call operator template specialization. If the generic lambda
|
| 353 |
+
has no *trailing-return-type* or the *trailing-return-type* contains a
|
| 354 |
+
placeholder type, return type deduction of the corresponding function
|
| 355 |
+
call operator template specialization has to be done. The corresponding
|
| 356 |
+
specialization is that instantiation of the function call operator
|
| 357 |
+
template with the same template arguments as those deduced for the
|
| 358 |
+
conversion function template. Consider the following:
|
| 359 |
+
|
| 360 |
+
``` cpp
|
| 361 |
+
auto glambda = [](auto a) { return a; };
|
| 362 |
+
int (*fp)(int) = glambda;
|
| 363 |
+
```
|
| 364 |
+
|
| 365 |
+
The behavior of the conversion function of `glambda` above is like that
|
| 366 |
+
of the following conversion function:
|
| 367 |
+
|
| 368 |
+
``` cpp
|
| 369 |
+
struct Closure {
|
| 370 |
+
template<class T> auto operator()(T t) const { ... }
|
| 371 |
+
template<class T> static auto lambda_call_operator_invoker(T a) {
|
| 372 |
+
// forwards execution to operator()(a) and therefore has
|
| 373 |
+
// the same return type deduced
|
| 374 |
+
...
|
| 375 |
+
}
|
| 376 |
+
template<class T> using fptr_t =
|
| 377 |
+
decltype(lambda_call_operator_invoker(declval<T>())) (*)(T);
|
| 378 |
+
|
| 379 |
+
template<class T> operator fptr_t<T>() const
|
| 380 |
+
{ return &lambda_call_operator_invoker; }
|
| 381 |
+
};
|
| 382 |
+
```
|
| 383 |
+
|
| 384 |
+
``` cpp
|
| 385 |
+
void f1(int (*)(int)) { }
|
| 386 |
+
void f2(char (*)(int)) { }
|
| 387 |
+
|
| 388 |
+
void g(int (*)(int)) { } // #1
|
| 389 |
+
void g(char (*)(char)) { } // #2
|
| 390 |
+
|
| 391 |
+
void h(int (*)(int)) { } // #3
|
| 392 |
+
void h(char (*)(int)) { } // #4
|
| 393 |
+
|
| 394 |
+
auto glambda = [](auto a) { return a; };
|
| 395 |
+
f1(glambda); // OK
|
| 396 |
+
f2(glambda); // error: ID is not convertible
|
| 397 |
+
g(glambda); // error: ambiguous
|
| 398 |
+
h(glambda); // OK: calls #3 since it is convertible from ID
|
| 399 |
+
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
| 400 |
+
```
|
| 401 |
+
|
| 402 |
+
The value returned by any given specialization of this conversion
|
| 403 |
+
function template shall be the address of a function that, when invoked,
|
| 404 |
+
has the same effect as invoking the generic lambda’s corresponding
|
| 405 |
+
function call operator template specialization. This will result in the
|
| 406 |
+
implicit instantiation of the generic lambda’s body. The instantiated
|
| 407 |
+
generic lambda’s return type and parameter types shall match the return
|
| 408 |
+
type and parameter types of the pointer to function.
|
| 409 |
+
|
| 410 |
+
``` cpp
|
| 411 |
+
auto GL = [](auto a) { std::cout << a; return a; };
|
| 412 |
+
int (*GL_int)(int) = GL; // OK: through conversion function template
|
| 413 |
+
GL_int(3); // OK: same as GL(3)
|
| 414 |
+
```
|
| 415 |
|
| 416 |
The *lambda-expression*’s *compound-statement* yields the
|
| 417 |
*function-body* ([[dcl.fct.def]]) of the function call operator, but
|
| 418 |
for purposes of name lookup ([[basic.lookup]]), determining the type
|
| 419 |
and value of `this` ([[class.this]]) and transforming *id-expression*s
|
|
|
|
| 433 |
};
|
| 434 |
}
|
| 435 |
};
|
| 436 |
```
|
| 437 |
|
| 438 |
+
Further, a variable `__func__` is implicitly defined at the beginning of
|
| 439 |
+
the *compound-statement* of the *lambda-expression*, with semantics as
|
| 440 |
+
described in [[dcl.fct.def.general]].
|
| 441 |
+
|
| 442 |
+
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 443 |
+
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 444 |
+
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 445 |
+
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
| 446 |
+
form “`&` *identifier*”. Ignoring appearances in *initializer*s of
|
| 447 |
+
*init-capture*s, an identifier or `this` shall not appear more than once
|
| 448 |
+
in a *lambda-capture*.
|
| 449 |
|
| 450 |
``` cpp
|
| 451 |
struct S2 { void f(int i); };
|
| 452 |
void S2::f(int i) {
|
| 453 |
[&, i]{ }; // OK
|
|
|
|
| 456 |
[i, i]{ }; // error: i repeated
|
| 457 |
}
|
| 458 |
```
|
| 459 |
|
| 460 |
A *lambda-expression* whose smallest enclosing scope is a block scope (
|
| 461 |
+
[[basic.scope.block]]) is a *local lambda expression*; any other
|
| 462 |
+
*lambda-expression* shall not have a *capture-default* or
|
| 463 |
+
*simple-capture* in its *lambda-introducer*. The *reaching scope* of a
|
| 464 |
+
local lambda expression is the set of enclosing scopes up to and
|
| 465 |
+
including the innermost enclosing function and its parameters. This
|
| 466 |
+
reaching scope includes any intervening *lambda-expression*s.
|
| 467 |
|
| 468 |
+
The *identifier* in a *simple-capture* is looked up using the usual
|
| 469 |
rules for unqualified name lookup ([[basic.lookup.unqual]]); each such
|
| 470 |
+
lookup shall find an entity. An entity that is designated by a
|
| 471 |
+
*simple-capture* is said to be *explicitly captured*, and shall be
|
| 472 |
+
`this` or a variable with automatic storage duration declared in the
|
| 473 |
+
reaching scope of the local lambda expression.
|
| 474 |
+
|
| 475 |
+
An *init-capture* behaves as if it declares and explicitly captures a
|
| 476 |
+
variable of the form “`auto` *init-capture* `;`” whose declarative
|
| 477 |
+
region is the *lambda-expression*’s *compound-statement*, except that:
|
| 478 |
+
|
| 479 |
+
- if the capture is by copy (see below), the non-static data member
|
| 480 |
+
declared for the capture and the variable are treated as two different
|
| 481 |
+
ways of referring to the same object, which has the lifetime of the
|
| 482 |
+
non-static data member, and no additional copy and destruction is
|
| 483 |
+
performed, and
|
| 484 |
+
- if the capture is by reference, the variable’s lifetime ends when the
|
| 485 |
+
closure object’s lifetime ends.
|
| 486 |
+
|
| 487 |
+
This enables an *init-capture* like “`x = std::move(x)`”; the second
|
| 488 |
+
“`x`” must bind to a declaration in the surrounding context.
|
| 489 |
+
|
| 490 |
+
``` cpp
|
| 491 |
+
int x = 4;
|
| 492 |
+
auto y = [&r = x, x = x+1]()->int {
|
| 493 |
+
r += 2;
|
| 494 |
+
return x+2;
|
| 495 |
+
}(); // Updates ::x to 6, and initializes y to 7.
|
| 496 |
+
```
|
| 497 |
+
|
| 498 |
+
A *lambda-expression* with an associated *capture-default* that does not
|
| 499 |
+
explicitly capture `this` or a variable with automatic storage duration
|
| 500 |
+
(this excludes any *id-expression* that has been found to refer to an
|
| 501 |
+
*init-capture*'s associated non-static data member), is said to
|
| 502 |
+
*implicitly capture* the entity (i.e., `this` or a variable) if the
|
| 503 |
+
*compound-statement*:
|
| 504 |
+
|
| 505 |
+
- odr-uses ([[basic.def.odr]]) the entity, or
|
| 506 |
+
- names the entity in a potentially-evaluated expression (
|
| 507 |
+
[[basic.def.odr]]) where the enclosing full-expression depends on a
|
| 508 |
+
generic lambda parameter declared within the reaching scope of the
|
| 509 |
+
*lambda-expression*.
|
| 510 |
+
|
| 511 |
+
``` cpp
|
| 512 |
+
void f(int, const int (&)[2] = {}) { } // #1
|
| 513 |
+
void f(const int&, const int (&)[1]) { } // #2
|
| 514 |
+
void test() {
|
| 515 |
+
const int x = 17;
|
| 516 |
+
auto g = [](auto a) {
|
| 517 |
+
f(x); // OK: calls #1, does not capture x
|
| 518 |
+
};
|
| 519 |
+
|
| 520 |
+
auto g2 = [=](auto a) {
|
| 521 |
+
int selector[sizeof(a) == 1 ? 1 : 2]{};
|
| 522 |
+
f(x, selector); // OK: is a dependent expression, so captures x
|
| 523 |
+
};
|
| 524 |
+
}
|
| 525 |
+
```
|
| 526 |
+
|
| 527 |
+
All such implicitly captured entities shall be declared within the
|
| 528 |
+
reaching scope of the lambda expression. The implicit capture of an
|
| 529 |
+
entity by a nested *lambda-expression* can cause its implicit capture by
|
| 530 |
+
the containing *lambda-expression* (see below). Implicit odr-uses of
|
| 531 |
+
`this` can result in implicit capture.
|
| 532 |
|
| 533 |
An entity is *captured* if it is captured explicitly or implicitly. An
|
| 534 |
entity captured by a *lambda-expression* is odr-used (
|
| 535 |
[[basic.def.odr]]) in the scope containing the *lambda-expression*. If
|
| 536 |
`this` is captured by a local lambda expression, its nearest enclosing
|
| 537 |
function shall be a non-static member function. If a *lambda-expression*
|
| 538 |
+
or an instantiation of the function call operator template of a generic
|
| 539 |
+
lambda odr-uses ([[basic.def.odr]]) `this` or a variable with automatic
|
| 540 |
storage duration from its reaching scope, that entity shall be captured
|
| 541 |
by the *lambda-expression*. If a *lambda-expression* captures an entity
|
| 542 |
and that entity is not defined or captured in the immediately enclosing
|
| 543 |
lambda expression or function, the program is ill-formed.
|
| 544 |
|
|
|
|
| 588 |
}
|
| 589 |
```
|
| 590 |
|
| 591 |
An entity is *captured by copy* if it is implicitly captured and the
|
| 592 |
*capture-default* is `=` or if it is explicitly captured with a capture
|
| 593 |
+
that is not of the form `&` *identifier* or `&` *identifier*
|
| 594 |
+
*initializer*. For each entity captured by copy, an unnamed non-static
|
| 595 |
+
data member is declared in the closure type. The declaration order of
|
| 596 |
+
these members is unspecified. The type of such a data member is the type
|
| 597 |
+
of the corresponding captured entity if the entity is not a reference to
|
| 598 |
+
an object, or the referenced type otherwise. If the captured entity is a
|
| 599 |
+
reference to a function, the corresponding data member is also a
|
| 600 |
+
reference to a function. A member of an anonymous union shall not be
|
| 601 |
+
captured by copy.
|
| 602 |
|
| 603 |
An entity is *captured by reference* if it is implicitly or explicitly
|
| 604 |
captured but not captured by copy. It is unspecified whether additional
|
| 605 |
unnamed non-static data members are declared in the closure type for
|
| 606 |
+
entities captured by reference. A member of an anonymous union shall not
|
| 607 |
+
be captured by reference.
|
| 608 |
|
| 609 |
If a *lambda-expression* `m2` captures an entity and that entity is
|
| 610 |
captured by an immediately enclosing *lambda-expression* `m1`, then
|
| 611 |
`m2`’s capture is transformed as follows:
|
| 612 |
|
|
|
|
| 631 |
a = 2; b = 2; c = 2;
|
| 632 |
m1();
|
| 633 |
std::cout << a << b << c;
|
| 634 |
```
|
| 635 |
|
| 636 |
+
Every *id-expression* within the *compound-statement* of a
|
| 637 |
+
*lambda-expression* that is an odr-use ([[basic.def.odr]]) of an entity
|
| 638 |
+
captured by copy is transformed into an access to the corresponding
|
| 639 |
+
unnamed data member of the closure type. An *id-expression* that is not
|
| 640 |
+
an odr-use refers to the original entity, never to a member of the
|
| 641 |
+
closure type. Furthermore, such an *id-expression* does not cause the
|
| 642 |
+
implicit capture of the entity. If `this` is captured, each odr-use of
|
| 643 |
+
`this` is transformed into an access to the corresponding unnamed data
|
| 644 |
+
member of the closure type, cast ([[expr.cast]]) to the type of `this`.
|
| 645 |
+
The cast ensures that the transformed expression is a prvalue.
|
| 646 |
|
| 647 |
``` cpp
|
| 648 |
void f(const int*);
|
| 649 |
void g() {
|
| 650 |
const int N = 10;
|
| 651 |
[=] {
|
| 652 |
int arr[N]; // OK: not an odr-use, refers to automatic variable
|
| 653 |
f(&N); // OK: causes N to be captured; &N points to the
|
| 654 |
// corresponding member of the closure type
|
| 655 |
+
};
|
| 656 |
}
|
| 657 |
```
|
| 658 |
|
| 659 |
Every occurrence of `decltype((x))` where `x` is a possibly
|
| 660 |
parenthesized *id-expression* that names an entity of automatic storage
|
|
|
|
| 686 |
The closure type associated with a *lambda-expression* has an
|
| 687 |
implicitly-declared destructor ([[class.dtor]]).
|
| 688 |
|
| 689 |
When the *lambda-expression* is evaluated, the entities that are
|
| 690 |
captured by copy are used to direct-initialize each corresponding
|
| 691 |
+
non-static data member of the resulting closure object, and the
|
| 692 |
+
non-static data members corresponding to the *init-capture*s are
|
| 693 |
+
initialized as indicated by the corresponding *initializer* (which may
|
| 694 |
+
be copy- or direct-initialization). (For array members, the array
|
| 695 |
+
elements are direct-initialized in increasing subscript order.) These
|
| 696 |
+
initializations are performed in the (unspecified) order in which the
|
| 697 |
+
non-static data members are declared. This ensures that the destructions
|
| 698 |
+
will occur in the reverse order of the constructions.
|
| 699 |
|
| 700 |
If an entity is implicitly or explicitly captured by reference, invoking
|
| 701 |
the function call operator of the corresponding *lambda-expression*
|
| 702 |
after the lifetime of the entity has ended is likely to result in
|
| 703 |
undefined behavior.
|
| 704 |
|
| 705 |
+
A *simple-capture* followed by an ellipsis is a pack expansion (
|
| 706 |
+
[[temp.variadic]]). An *init-capture* followed by an ellipsis is
|
| 707 |
+
ill-formed.
|
| 708 |
|
| 709 |
``` cpp
|
| 710 |
template<class... Args>
|
| 711 |
void f(Args... args) {
|
| 712 |
auto lm = [&, args...] { return g(args...); };
|