- tmp/tmp967eflqv/{from.md → to.md} +155 -79
tmp/tmp967eflqv/{from.md → to.md}
RENAMED
|
@@ -15,14 +15,15 @@ lambda-introducer:
|
|
| 15 |
```
|
| 16 |
|
| 17 |
``` bnf
|
| 18 |
lambda-declarator:
|
| 19 |
lambda-specifier-seq noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 20 |
-
|
| 21 |
-
trailing-return-typeₒₚₜ
|
|
|
|
| 22 |
'(' parameter-declaration-clause ')' lambda-specifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 23 |
-
trailing-return-typeₒₚₜ requires-clauseₒₚₜ
|
| 24 |
```
|
| 25 |
|
| 26 |
``` bnf
|
| 27 |
lambda-specifier:
|
| 28 |
consteval
|
|
@@ -31,12 +32,11 @@ lambda-specifier:
|
|
| 31 |
static
|
| 32 |
```
|
| 33 |
|
| 34 |
``` bnf
|
| 35 |
lambda-specifier-seq:
|
| 36 |
-
lambda-specifier
|
| 37 |
-
lambda-specifier lambda-specifier-seq
|
| 38 |
```
|
| 39 |
|
| 40 |
A *lambda-expression* provides a concise way to create a simple function
|
| 41 |
object.
|
| 42 |
|
|
@@ -62,12 +62,24 @@ An ambiguity can arise because a *requires-clause* can end in an
|
|
| 62 |
*attribute-specifier-seq*, which collides with the
|
| 63 |
*attribute-specifier-seq* in *lambda-expression*. In such cases, any
|
| 64 |
attributes are treated as *attribute-specifier-seq* in
|
| 65 |
*lambda-expression*.
|
| 66 |
|
| 67 |
-
[*Note 2*:
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
A *lambda-specifier-seq* shall contain at most one of each
|
| 71 |
*lambda-specifier* and shall not contain both `constexpr` and
|
| 72 |
`consteval`. If the *lambda-declarator* contains an explicit object
|
| 73 |
parameter [[dcl.fct]], then no *lambda-specifier* in the
|
|
@@ -77,19 +89,20 @@ the *lambda-specifier-seq* contains `static`, there shall be no
|
|
| 77 |
*lambda-capture*.
|
| 78 |
|
| 79 |
[*Note 3*: The trailing *requires-clause* is described in
|
| 80 |
[[dcl.decl]]. — *end note*]
|
| 81 |
|
| 82 |
-
|
| 83 |
-
*parameter-declaration-clause*
|
| 84 |
-
|
| 85 |
-
include a *trailing-return-type*, it is
|
|
|
|
| 86 |
|
| 87 |
[*Note 4*: In that case, the return type is deduced from `return`
|
| 88 |
statements as described in [[dcl.spec.auto]]. — *end note*]
|
| 89 |
|
| 90 |
-
[*Example
|
| 91 |
|
| 92 |
``` cpp
|
| 93 |
auto x1 = [](int i) { return i; }; // OK, return type is int
|
| 94 |
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 95 |
int j;
|
|
@@ -100,53 +113,62 @@ auto x3 = [&]()->auto&& { return j; }; // OK, return type is int&
|
|
| 100 |
|
| 101 |
A lambda is a *generic lambda* if the *lambda-expression* has any
|
| 102 |
generic parameter type placeholders [[dcl.spec.auto]], or if the lambda
|
| 103 |
has a *template-parameter-list*.
|
| 104 |
|
| 105 |
-
[*Example
|
| 106 |
|
| 107 |
``` cpp
|
| 108 |
-
|
| 109 |
-
|
|
|
|
| 110 |
```
|
| 111 |
|
| 112 |
— *end example*]
|
| 113 |
|
| 114 |
#### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
|
| 115 |
|
| 116 |
The type of a *lambda-expression* (which is also the type of the closure
|
| 117 |
object) is a unique, unnamed non-union class type, called the *closure
|
| 118 |
type*, whose properties are described below.
|
| 119 |
|
|
|
|
|
|
|
|
|
|
| 120 |
The closure type is declared in the smallest block scope, class scope,
|
| 121 |
or namespace scope that contains the corresponding *lambda-expression*.
|
| 122 |
|
| 123 |
[*Note 1*: This determines the set of namespaces and classes associated
|
| 124 |
with the closure type [[basic.lookup.argdep]]. The parameter types of a
|
| 125 |
*lambda-declarator* do not affect these associated namespaces and
|
| 126 |
classes. — *end note*]
|
| 127 |
|
| 128 |
-
The closure type is not an aggregate type [[dcl.init.aggr]]
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
|
| 133 |
- the size and/or alignment of the closure type,
|
| 134 |
-
- whether the closure type is trivially copyable [[class.prop]],
|
|
|
|
|
|
|
| 135 |
- whether the closure type is a standard-layout class [[class.prop]].
|
| 136 |
|
| 137 |
An implementation shall not add members of rvalue reference type to the
|
| 138 |
closure type.
|
| 139 |
|
| 140 |
The closure type for a *lambda-expression* has a public inline function
|
| 141 |
call operator (for a non-generic lambda) or function call operator
|
| 142 |
template (for a generic lambda) [[over.call]] whose parameters and
|
| 143 |
-
return type are
|
| 144 |
*parameter-declaration-clause* and *trailing-return-type* respectively,
|
| 145 |
and whose *template-parameter-list* consists of the specified
|
| 146 |
-
*template-parameter-list*, if any. The
|
| 147 |
-
call operator template
|
|
|
|
|
|
|
| 148 |
`<` *template-parameter-list* `>`, if any. The trailing
|
| 149 |
*requires-clause* of the function call operator or operator template is
|
| 150 |
the *requires-clause* of the *lambda-declarator*, if any.
|
| 151 |
|
| 152 |
[*Note 2*: The function call operator template for a generic lambda can
|
|
@@ -183,11 +205,12 @@ std::cout << fact(5); // OK, outputs 1
|
|
| 183 |
Given a lambda with a *lambda-capture*, the type of the explicit object
|
| 184 |
parameter, if any, of the lambda’s function call operator (possibly
|
| 185 |
instantiated from a function call operator template) shall be either:
|
| 186 |
|
| 187 |
- the closure type,
|
| 188 |
-
- a class type derived from the closure type,
|
|
|
|
| 189 |
- a reference to a possibly cv-qualified such type.
|
| 190 |
|
| 191 |
[*Example 2*:
|
| 192 |
|
| 193 |
``` cpp
|
|
@@ -211,13 +234,14 @@ function or static member function template [[class.static.mfct]] if the
|
|
| 211 |
`static`. Otherwise, it is a non-static member function or member
|
| 212 |
function template [[class.mfct.non.static]] that is declared `const`
|
| 213 |
[[class.mfct.non.static]] if and only if the *lambda-expression*’s
|
| 214 |
*parameter-declaration-clause* is not followed by `mutable` and the
|
| 215 |
*lambda-declarator* does not contain an explicit object parameter. It is
|
| 216 |
-
neither virtual nor declared `volatile`. Any *noexcept-specifier*
|
| 217 |
-
|
| 218 |
-
|
|
|
|
| 219 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 220 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 221 |
*lambda-expression* preceding a *lambda-declarator* appertains to the
|
| 222 |
corresponding function call operator or operator template. The function
|
| 223 |
call operator or any given operator template specialization is a
|
|
@@ -296,35 +320,80 @@ auto f = []<typename T1, C1 T2> requires C2<sizeof(T1) + sizeof(T2)>
|
|
| 296 |
|
| 297 |
— *end example*]
|
| 298 |
|
| 299 |
— *end note*]
|
| 300 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
The closure type for a non-generic *lambda-expression* with no
|
| 302 |
-
*lambda-capture*
|
| 303 |
-
|
| 304 |
-
[[dcl.link]] having the same
|
| 305 |
-
type’s function call operator.
|
| 306 |
-
`noexcept` function” if the function
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
invoked, has the same effect as
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
function
|
| 315 |
-
|
|
|
|
| 316 |
|
| 317 |
-
For a generic lambda with no *lambda-capture*
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
|
|
|
| 324 |
|
| 325 |
-
[*Note
|
| 326 |
|
| 327 |
If the generic lambda has no *trailing-return-type* or the
|
| 328 |
*trailing-return-type* contains a placeholder type, return type
|
| 329 |
deduction of the corresponding function call operator template
|
| 330 |
specialization has to be done. The corresponding specialization is that
|
|
@@ -356,11 +425,11 @@ struct Closure {
|
|
| 356 |
};
|
| 357 |
```
|
| 358 |
|
| 359 |
— *end note*]
|
| 360 |
|
| 361 |
-
[*Example
|
| 362 |
|
| 363 |
``` cpp
|
| 364 |
void f1(int (*)(int)) { }
|
| 365 |
void f2(char (*)(int)) { }
|
| 366 |
|
|
@@ -380,27 +449,26 @@ int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
|
|
| 380 |
|
| 381 |
— *end example*]
|
| 382 |
|
| 383 |
If the function call operator template is a static member function
|
| 384 |
template, then the value returned by any given specialization of this
|
| 385 |
-
conversion function template is
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
function.
|
| 395 |
|
| 396 |
-
[*Note
|
| 397 |
generic lambda’s body. The instantiated generic lambda’s return type and
|
| 398 |
-
parameter types
|
| 399 |
-
|
| 400 |
|
| 401 |
-
[*Example
|
| 402 |
|
| 403 |
``` cpp
|
| 404 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 405 |
int (*GL_int)(int) = GL; // OK, through conversion function template
|
| 406 |
GL_int(3); // OK, same as GL(3)
|
|
@@ -410,11 +478,11 @@ GL_int(3); // OK, same as GL(3)
|
|
| 410 |
|
| 411 |
The conversion function or conversion function template is public,
|
| 412 |
constexpr, non-virtual, non-explicit, const, and has a non-throwing
|
| 413 |
exception specification [[except.spec]].
|
| 414 |
|
| 415 |
-
[*Example
|
| 416 |
|
| 417 |
``` cpp
|
| 418 |
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
|
| 419 |
auto C = [](auto a) { return a; };
|
| 420 |
|
|
@@ -429,11 +497,11 @@ static_assert(Fwd(NC,3) == 3); // error
|
|
| 429 |
|
| 430 |
The *lambda-expression*’s *compound-statement* yields the
|
| 431 |
*function-body* [[dcl.fct.def]] of the function call operator, but it is
|
| 432 |
not within the scope of the closure type.
|
| 433 |
|
| 434 |
-
[*Example
|
| 435 |
|
| 436 |
``` cpp
|
| 437 |
struct S1 {
|
| 438 |
int x, y;
|
| 439 |
int operator()(int);
|
|
@@ -446,23 +514,25 @@ struct S1 {
|
|
| 446 |
};
|
| 447 |
```
|
| 448 |
|
| 449 |
— *end example*]
|
| 450 |
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
| 454 |
|
| 455 |
The closure type associated with a *lambda-expression* has no default
|
| 456 |
constructor if the *lambda-expression* has a *lambda-capture* and a
|
| 457 |
defaulted default constructor otherwise. It has a defaulted copy
|
| 458 |
constructor and a defaulted move constructor [[class.copy.ctor]]. It has
|
| 459 |
a deleted copy assignment operator if the *lambda-expression* has a
|
| 460 |
*lambda-capture* and defaulted copy and move assignment operators
|
| 461 |
otherwise [[class.copy.assign]].
|
| 462 |
|
| 463 |
-
[*Note
|
| 464 |
usual, which can result in them being defined as deleted. — *end note*]
|
| 465 |
|
| 466 |
The closure type associated with a *lambda-expression* has an
|
| 467 |
implicitly-declared destructor [[class.dtor]].
|
| 468 |
|
|
@@ -500,30 +570,30 @@ capture:
|
|
| 500 |
``` bnf
|
| 501 |
simple-capture:
|
| 502 |
identifier '...'ₒₚₜ
|
| 503 |
'&' identifier '...'ₒₚₜ
|
| 504 |
this
|
| 505 |
-
'*'
|
| 506 |
```
|
| 507 |
|
| 508 |
``` bnf
|
| 509 |
init-capture:
|
| 510 |
'...'ₒₚₜ identifier initializer
|
| 511 |
'&' '...'ₒₚₜ identifier initializer
|
| 512 |
```
|
| 513 |
|
| 514 |
The body of a *lambda-expression* may refer to local entities of
|
| 515 |
-
enclosing
|
| 516 |
|
| 517 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 518 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 519 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 520 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
| 521 |
form “`&` *identifier* `...`ₒₚₜ ”, “`this`”, or “`* this`”.
|
| 522 |
|
| 523 |
[*Note 1*: The form `[&,this]` is redundant but accepted for
|
| 524 |
-
compatibility with
|
| 525 |
|
| 526 |
Ignoring appearances in *initializer*s of *init-capture*s, an identifier
|
| 527 |
or `this` shall not appear more than once in a *lambda-capture*.
|
| 528 |
|
| 529 |
[*Example 1*:
|
|
@@ -542,14 +612,19 @@ void S2::f(int i) {
|
|
| 542 |
```
|
| 543 |
|
| 544 |
— *end example*]
|
| 545 |
|
| 546 |
A *lambda-expression* shall not have a *capture-default* or
|
| 547 |
-
*simple-capture* in its *lambda-introducer* unless
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
|
| 552 |
The *identifier* in a *simple-capture* shall denote a local entity
|
| 553 |
[[basic.lookup.unqual]], [[basic.pre]]. The *simple-capture*s `this` and
|
| 554 |
`* this` denote the local entity `*this`. An entity that is designated
|
| 555 |
by a *simple-capture* is said to be *explicitly captured*.
|
|
@@ -767,11 +842,12 @@ void f2() {
|
|
| 767 |
An entity is *captured by copy* if
|
| 768 |
|
| 769 |
- it is implicitly captured, the *capture-default* is `=`, and the
|
| 770 |
captured entity is not `*this`, or
|
| 771 |
- it is explicitly captured with a capture that is not of the form
|
| 772 |
-
`this`, `&` *identifier*, or `&` *identifier*
|
|
|
|
| 773 |
|
| 774 |
For each entity captured by copy, an unnamed non-static data member is
|
| 775 |
declared in the closure type. The declaration order of these members is
|
| 776 |
unspecified. The type of such a data member is the referenced type if
|
| 777 |
the entity is a reference to an object, an lvalue reference to the
|
|
@@ -798,11 +874,11 @@ the closure type.
|
|
| 798 |
``` cpp
|
| 799 |
void f(const int*);
|
| 800 |
void g() {
|
| 801 |
const int N = 10;
|
| 802 |
[=] {
|
| 803 |
-
int arr[N]; // OK, not an odr-use, refers to automatic
|
| 804 |
f(&N); // OK, causes N to be captured; &N points to
|
| 805 |
// the corresponding member of the closure type
|
| 806 |
};
|
| 807 |
}
|
| 808 |
```
|
|
|
|
| 15 |
```
|
| 16 |
|
| 17 |
``` bnf
|
| 18 |
lambda-declarator:
|
| 19 |
lambda-specifier-seq noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ
|
| 20 |
+
function-contract-specifier-seqₒₚₜ
|
| 21 |
+
noexcept-specifier attribute-specifier-seqₒₚₜ trailing-return-typeₒₚₜ function-contract-specifier-seqₒₚₜ
|
| 22 |
+
trailing-return-typeₒₚₜ function-contract-specifier-seqₒₚₜ
|
| 23 |
'(' parameter-declaration-clause ')' lambda-specifier-seqₒₚₜ noexcept-specifierₒₚₜ attribute-specifier-seqₒₚₜ
|
| 24 |
+
trailing-return-typeₒₚₜ requires-clauseₒₚₜ function-contract-specifier-seqₒₚₜ
|
| 25 |
```
|
| 26 |
|
| 27 |
``` bnf
|
| 28 |
lambda-specifier:
|
| 29 |
consteval
|
|
|
|
| 32 |
static
|
| 33 |
```
|
| 34 |
|
| 35 |
``` bnf
|
| 36 |
lambda-specifier-seq:
|
| 37 |
+
lambda-specifier lambda-specifier-seqₒₚₜ
|
|
|
|
| 38 |
```
|
| 39 |
|
| 40 |
A *lambda-expression* provides a concise way to create a simple function
|
| 41 |
object.
|
| 42 |
|
|
|
|
| 62 |
*attribute-specifier-seq*, which collides with the
|
| 63 |
*attribute-specifier-seq* in *lambda-expression*. In such cases, any
|
| 64 |
attributes are treated as *attribute-specifier-seq* in
|
| 65 |
*lambda-expression*.
|
| 66 |
|
| 67 |
+
[*Note 2*:
|
| 68 |
+
|
| 69 |
+
Such ambiguous cases cannot have valid semantics because the constraint
|
| 70 |
+
expression would not have type `bool`.
|
| 71 |
+
|
| 72 |
+
[*Example 2*:
|
| 73 |
+
|
| 74 |
+
``` cpp
|
| 75 |
+
auto x = []<class T> requires T::operator int [[some_attribute]] (int) { }
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
— *end example*]
|
| 79 |
+
|
| 80 |
+
— *end note*]
|
| 81 |
|
| 82 |
A *lambda-specifier-seq* shall contain at most one of each
|
| 83 |
*lambda-specifier* and shall not contain both `constexpr` and
|
| 84 |
`consteval`. If the *lambda-declarator* contains an explicit object
|
| 85 |
parameter [[dcl.fct]], then no *lambda-specifier* in the
|
|
|
|
| 89 |
*lambda-capture*.
|
| 90 |
|
| 91 |
[*Note 3*: The trailing *requires-clause* is described in
|
| 92 |
[[dcl.decl]]. — *end note*]
|
| 93 |
|
| 94 |
+
A *lambda-expression*'s *parameter-declaration-clause* is the
|
| 95 |
+
*parameter-declaration-clause* of the *lambda-expression*'s
|
| 96 |
+
*lambda-declarator*, if any, or empty otherwise. If the
|
| 97 |
+
*lambda-declarator* does not include a *trailing-return-type*, it is
|
| 98 |
+
considered to be `-> auto`.
|
| 99 |
|
| 100 |
[*Note 4*: In that case, the return type is deduced from `return`
|
| 101 |
statements as described in [[dcl.spec.auto]]. — *end note*]
|
| 102 |
|
| 103 |
+
[*Example 3*:
|
| 104 |
|
| 105 |
``` cpp
|
| 106 |
auto x1 = [](int i) { return i; }; // OK, return type is int
|
| 107 |
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from braced-init-list
|
| 108 |
int j;
|
|
|
|
| 113 |
|
| 114 |
A lambda is a *generic lambda* if the *lambda-expression* has any
|
| 115 |
generic parameter type placeholders [[dcl.spec.auto]], or if the lambda
|
| 116 |
has a *template-parameter-list*.
|
| 117 |
|
| 118 |
+
[*Example 4*:
|
| 119 |
|
| 120 |
``` cpp
|
| 121 |
+
auto x = [](int i, auto a) { return i; }; // OK, a generic lambda
|
| 122 |
+
auto y = [](this auto self, int i) { return i; }; // OK, a generic lambda
|
| 123 |
+
auto z = []<class T>(int i) { return i; }; // OK, a generic lambda
|
| 124 |
```
|
| 125 |
|
| 126 |
— *end example*]
|
| 127 |
|
| 128 |
#### Closure types <a id="expr.prim.lambda.closure">[[expr.prim.lambda.closure]]</a>
|
| 129 |
|
| 130 |
The type of a *lambda-expression* (which is also the type of the closure
|
| 131 |
object) is a unique, unnamed non-union class type, called the *closure
|
| 132 |
type*, whose properties are described below.
|
| 133 |
|
| 134 |
+
The closure type is incomplete until the end of its corresponding
|
| 135 |
+
*compound-statement*.
|
| 136 |
+
|
| 137 |
The closure type is declared in the smallest block scope, class scope,
|
| 138 |
or namespace scope that contains the corresponding *lambda-expression*.
|
| 139 |
|
| 140 |
[*Note 1*: This determines the set of namespaces and classes associated
|
| 141 |
with the closure type [[basic.lookup.argdep]]. The parameter types of a
|
| 142 |
*lambda-declarator* do not affect these associated namespaces and
|
| 143 |
classes. — *end note*]
|
| 144 |
|
| 145 |
+
The closure type is not an aggregate type [[dcl.init.aggr]]; it is a
|
| 146 |
+
structural type [[term.structural.type]] if and only if the lambda has
|
| 147 |
+
no *lambda-capture*. An implementation may define the closure type
|
| 148 |
+
differently from what is described below provided this does not alter
|
| 149 |
+
the observable behavior of the program other than by changing:
|
| 150 |
|
| 151 |
- the size and/or alignment of the closure type,
|
| 152 |
+
- whether the closure type is trivially copyable [[class.prop]],
|
| 153 |
+
- whether the closure type is trivially relocatable [[class.prop]],
|
| 154 |
+
- whether the closure type is replaceable [[class.prop]], or
|
| 155 |
- whether the closure type is a standard-layout class [[class.prop]].
|
| 156 |
|
| 157 |
An implementation shall not add members of rvalue reference type to the
|
| 158 |
closure type.
|
| 159 |
|
| 160 |
The closure type for a *lambda-expression* has a public inline function
|
| 161 |
call operator (for a non-generic lambda) or function call operator
|
| 162 |
template (for a generic lambda) [[over.call]] whose parameters and
|
| 163 |
+
return type are those of the *lambda-expression*'s
|
| 164 |
*parameter-declaration-clause* and *trailing-return-type* respectively,
|
| 165 |
and whose *template-parameter-list* consists of the specified
|
| 166 |
+
*template-parameter-list*, if any. The function call operator or the
|
| 167 |
+
function call operator template are direct members of the closure type.
|
| 168 |
+
The *requires-clause* of the function call operator template is the
|
| 169 |
+
*requires-clause* immediately following
|
| 170 |
`<` *template-parameter-list* `>`, if any. The trailing
|
| 171 |
*requires-clause* of the function call operator or operator template is
|
| 172 |
the *requires-clause* of the *lambda-declarator*, if any.
|
| 173 |
|
| 174 |
[*Note 2*: The function call operator template for a generic lambda can
|
|
|
|
| 205 |
Given a lambda with a *lambda-capture*, the type of the explicit object
|
| 206 |
parameter, if any, of the lambda’s function call operator (possibly
|
| 207 |
instantiated from a function call operator template) shall be either:
|
| 208 |
|
| 209 |
- the closure type,
|
| 210 |
+
- a class type publicly and unambiguously derived from the closure type,
|
| 211 |
+
or
|
| 212 |
- a reference to a possibly cv-qualified such type.
|
| 213 |
|
| 214 |
[*Example 2*:
|
| 215 |
|
| 216 |
``` cpp
|
|
|
|
| 234 |
`static`. Otherwise, it is a non-static member function or member
|
| 235 |
function template [[class.mfct.non.static]] that is declared `const`
|
| 236 |
[[class.mfct.non.static]] if and only if the *lambda-expression*’s
|
| 237 |
*parameter-declaration-clause* is not followed by `mutable` and the
|
| 238 |
*lambda-declarator* does not contain an explicit object parameter. It is
|
| 239 |
+
neither virtual nor declared `volatile`. Any *noexcept-specifier* or
|
| 240 |
+
*function-contract-specifier* [[dcl.contract.func]] specified on a
|
| 241 |
+
*lambda-expression* applies to the corresponding function call operator
|
| 242 |
+
or operator template. An *attribute-specifier-seq* in a
|
| 243 |
*lambda-declarator* appertains to the type of the corresponding function
|
| 244 |
call operator or operator template. An *attribute-specifier-seq* in a
|
| 245 |
*lambda-expression* preceding a *lambda-declarator* appertains to the
|
| 246 |
corresponding function call operator or operator template. The function
|
| 247 |
call operator or any given operator template specialization is a
|
|
|
|
| 320 |
|
| 321 |
— *end example*]
|
| 322 |
|
| 323 |
— *end note*]
|
| 324 |
|
| 325 |
+
If all potential references to a local entity implicitly captured by a
|
| 326 |
+
*lambda-expression* L occur within the function contract assertions
|
| 327 |
+
[[dcl.contract.func]] of the call operator or operator template of L or
|
| 328 |
+
within *assertion-statement*s [[stmt.contract.assert]] within the body
|
| 329 |
+
of L, the program is ill-formed.
|
| 330 |
+
|
| 331 |
+
[*Note 4*: Adding a contract assertion to an existing C++ program
|
| 332 |
+
cannot cause additional captures. — *end note*]
|
| 333 |
+
|
| 334 |
+
[*Example 6*:
|
| 335 |
+
|
| 336 |
+
``` cpp
|
| 337 |
+
static int i = 0;
|
| 338 |
+
|
| 339 |
+
void test() {
|
| 340 |
+
auto f1 = [=] pre(i > 0) {}; // OK, no local entities are captured.
|
| 341 |
+
|
| 342 |
+
int i = 1;
|
| 343 |
+
auto f2 = [=] pre(i > 0) {}; // error: cannot implicitly capture i here
|
| 344 |
+
auto f3 = [i] pre(i > 0) {}; // OK, i is captured explicitly.
|
| 345 |
+
|
| 346 |
+
auto f4 = [=] {
|
| 347 |
+
contract_assert(i > 0); // error: cannot implicitly capture i here
|
| 348 |
+
};
|
| 349 |
+
|
| 350 |
+
auto f5 = [=] {
|
| 351 |
+
contract_assert(i > 0); // OK, i is referenced elsewhere.
|
| 352 |
+
(void)i;
|
| 353 |
+
};
|
| 354 |
+
|
| 355 |
+
auto f6 = [=] pre( // #1
|
| 356 |
+
[]{
|
| 357 |
+
bool x = true;
|
| 358 |
+
return [=]{ return x; }(); // OK, #1 captures nothing.
|
| 359 |
+
}()) {};
|
| 360 |
+
|
| 361 |
+
bool y = true;
|
| 362 |
+
auto f7 = [=] pre([=]{ return y; }()); // error: outer capture of y is invalid.
|
| 363 |
+
}
|
| 364 |
+
```
|
| 365 |
+
|
| 366 |
+
— *end example*]
|
| 367 |
+
|
| 368 |
The closure type for a non-generic *lambda-expression* with no
|
| 369 |
+
*lambda-capture* and no explicit object parameter [[dcl.fct]] whose
|
| 370 |
+
constraints (if any) are satisfied has a conversion function to pointer
|
| 371 |
+
to function with C++ language linkage [[dcl.link]] having the same
|
| 372 |
+
parameter and return types as the closure type’s function call operator.
|
| 373 |
+
The conversion is to “pointer to `noexcept` function” if the function
|
| 374 |
+
call operator has a non-throwing exception specification. If the
|
| 375 |
+
function call operator is a static member function, then the value
|
| 376 |
+
returned by this conversion function is a pointer to the function call
|
| 377 |
+
operator. Otherwise, the value returned by this conversion function is a
|
| 378 |
+
pointer to a function `F` that, when invoked, has the same effect as
|
| 379 |
+
invoking the closure type’s function call operator on a
|
| 380 |
+
default-constructed instance of the closure type. `F` is a constexpr
|
| 381 |
+
function if the function call operator is a constexpr function and is an
|
| 382 |
+
immediate function if the function call operator is an immediate
|
| 383 |
+
function.
|
| 384 |
|
| 385 |
+
For a generic lambda with no *lambda-capture* and no explicit object
|
| 386 |
+
parameter [[dcl.fct]], the closure type has a conversion function
|
| 387 |
+
template to pointer to function. The conversion function template has
|
| 388 |
+
the same invented template parameter list, and the pointer to function
|
| 389 |
+
has the same parameter types, as the function call operator template.
|
| 390 |
+
The return type of the pointer to function shall behave as if it were a
|
| 391 |
+
*decltype-specifier* denoting the return type of the corresponding
|
| 392 |
+
function call operator template specialization.
|
| 393 |
|
| 394 |
+
[*Note 5*:
|
| 395 |
|
| 396 |
If the generic lambda has no *trailing-return-type* or the
|
| 397 |
*trailing-return-type* contains a placeholder type, return type
|
| 398 |
deduction of the corresponding function call operator template
|
| 399 |
specialization has to be done. The corresponding specialization is that
|
|
|
|
| 425 |
};
|
| 426 |
```
|
| 427 |
|
| 428 |
— *end note*]
|
| 429 |
|
| 430 |
+
[*Example 7*:
|
| 431 |
|
| 432 |
``` cpp
|
| 433 |
void f1(int (*)(int)) { }
|
| 434 |
void f2(char (*)(int)) { }
|
| 435 |
|
|
|
|
| 449 |
|
| 450 |
— *end example*]
|
| 451 |
|
| 452 |
If the function call operator template is a static member function
|
| 453 |
template, then the value returned by any given specialization of this
|
| 454 |
+
conversion function template is a pointer to the corresponding function
|
| 455 |
+
call operator template specialization. Otherwise, the value returned by
|
| 456 |
+
any given specialization of this conversion function template is a
|
| 457 |
+
pointer to a function `F` that, when invoked, has the same effect as
|
| 458 |
+
invoking the generic lambda’s corresponding function call operator
|
| 459 |
+
template specialization on a default-constructed instance of the closure
|
| 460 |
+
type. `F` is a constexpr function if the corresponding specialization is
|
| 461 |
+
a constexpr function and `F` is an immediate function if the function
|
| 462 |
+
call operator template specialization is an immediate function.
|
|
|
|
| 463 |
|
| 464 |
+
[*Note 6*: This will result in the implicit instantiation of the
|
| 465 |
generic lambda’s body. The instantiated generic lambda’s return type and
|
| 466 |
+
parameter types need to match the return type and parameter types of the
|
| 467 |
+
pointer to function. — *end note*]
|
| 468 |
|
| 469 |
+
[*Example 8*:
|
| 470 |
|
| 471 |
``` cpp
|
| 472 |
auto GL = [](auto a) { std::cout << a; return a; };
|
| 473 |
int (*GL_int)(int) = GL; // OK, through conversion function template
|
| 474 |
GL_int(3); // OK, same as GL(3)
|
|
|
|
| 478 |
|
| 479 |
The conversion function or conversion function template is public,
|
| 480 |
constexpr, non-virtual, non-explicit, const, and has a non-throwing
|
| 481 |
exception specification [[except.spec]].
|
| 482 |
|
| 483 |
+
[*Example 9*:
|
| 484 |
|
| 485 |
``` cpp
|
| 486 |
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
|
| 487 |
auto C = [](auto a) { return a; };
|
| 488 |
|
|
|
|
| 497 |
|
| 498 |
The *lambda-expression*’s *compound-statement* yields the
|
| 499 |
*function-body* [[dcl.fct.def]] of the function call operator, but it is
|
| 500 |
not within the scope of the closure type.
|
| 501 |
|
| 502 |
+
[*Example 10*:
|
| 503 |
|
| 504 |
``` cpp
|
| 505 |
struct S1 {
|
| 506 |
int x, y;
|
| 507 |
int operator()(int);
|
|
|
|
| 514 |
};
|
| 515 |
```
|
| 516 |
|
| 517 |
— *end example*]
|
| 518 |
|
| 519 |
+
Unless the *compound-statement* is that of a
|
| 520 |
+
*consteval-block-declaration* [[dcl.pre]], a variable `__func__` is
|
| 521 |
+
implicitly defined at the beginning of the *compound-statement* of the
|
| 522 |
+
*lambda-expression*, with semantics as described in
|
| 523 |
+
[[dcl.fct.def.general]].
|
| 524 |
|
| 525 |
The closure type associated with a *lambda-expression* has no default
|
| 526 |
constructor if the *lambda-expression* has a *lambda-capture* and a
|
| 527 |
defaulted default constructor otherwise. It has a defaulted copy
|
| 528 |
constructor and a defaulted move constructor [[class.copy.ctor]]. It has
|
| 529 |
a deleted copy assignment operator if the *lambda-expression* has a
|
| 530 |
*lambda-capture* and defaulted copy and move assignment operators
|
| 531 |
otherwise [[class.copy.assign]].
|
| 532 |
|
| 533 |
+
[*Note 7*: These special member functions are implicitly defined as
|
| 534 |
usual, which can result in them being defined as deleted. — *end note*]
|
| 535 |
|
| 536 |
The closure type associated with a *lambda-expression* has an
|
| 537 |
implicitly-declared destructor [[class.dtor]].
|
| 538 |
|
|
|
|
| 570 |
``` bnf
|
| 571 |
simple-capture:
|
| 572 |
identifier '...'ₒₚₜ
|
| 573 |
'&' identifier '...'ₒₚₜ
|
| 574 |
this
|
| 575 |
+
'*' this
|
| 576 |
```
|
| 577 |
|
| 578 |
``` bnf
|
| 579 |
init-capture:
|
| 580 |
'...'ₒₚₜ identifier initializer
|
| 581 |
'&' '...'ₒₚₜ identifier initializer
|
| 582 |
```
|
| 583 |
|
| 584 |
The body of a *lambda-expression* may refer to local entities of
|
| 585 |
+
enclosing scopes by capturing those entities, as described below.
|
| 586 |
|
| 587 |
If a *lambda-capture* includes a *capture-default* that is `&`, no
|
| 588 |
identifier in a *simple-capture* of that *lambda-capture* shall be
|
| 589 |
preceded by `&`. If a *lambda-capture* includes a *capture-default* that
|
| 590 |
is `=`, each *simple-capture* of that *lambda-capture* shall be of the
|
| 591 |
form “`&` *identifier* `...`ₒₚₜ ”, “`this`”, or “`* this`”.
|
| 592 |
|
| 593 |
[*Note 1*: The form `[&,this]` is redundant but accepted for
|
| 594 |
+
compatibility with C++14. — *end note*]
|
| 595 |
|
| 596 |
Ignoring appearances in *initializer*s of *init-capture*s, an identifier
|
| 597 |
or `this` shall not appear more than once in a *lambda-capture*.
|
| 598 |
|
| 599 |
[*Example 1*:
|
|
|
|
| 612 |
```
|
| 613 |
|
| 614 |
— *end example*]
|
| 615 |
|
| 616 |
A *lambda-expression* shall not have a *capture-default* or
|
| 617 |
+
*simple-capture* in its *lambda-introducer* unless
|
| 618 |
+
|
| 619 |
+
- its innermost enclosing scope is a block scope [[basic.scope.block]],
|
| 620 |
+
- it appears within a default member initializer and its innermost
|
| 621 |
+
enclosing scope is the corresponding class scope
|
| 622 |
+
[[basic.scope.class]], or
|
| 623 |
+
- it appears within a contract assertion and its innermost enclosing
|
| 624 |
+
scope is the corresponding contract-assertion scope
|
| 625 |
+
[[basic.scope.contract]].
|
| 626 |
|
| 627 |
The *identifier* in a *simple-capture* shall denote a local entity
|
| 628 |
[[basic.lookup.unqual]], [[basic.pre]]. The *simple-capture*s `this` and
|
| 629 |
`* this` denote the local entity `*this`. An entity that is designated
|
| 630 |
by a *simple-capture* is said to be *explicitly captured*.
|
|
|
|
| 842 |
An entity is *captured by copy* if
|
| 843 |
|
| 844 |
- it is implicitly captured, the *capture-default* is `=`, and the
|
| 845 |
captured entity is not `*this`, or
|
| 846 |
- it is explicitly captured with a capture that is not of the form
|
| 847 |
+
`this`, `&` *identifier* `...`ₒₚₜ , or `&` `...`ₒₚₜ *identifier*
|
| 848 |
+
*initializer*.
|
| 849 |
|
| 850 |
For each entity captured by copy, an unnamed non-static data member is
|
| 851 |
declared in the closure type. The declaration order of these members is
|
| 852 |
unspecified. The type of such a data member is the referenced type if
|
| 853 |
the entity is a reference to an object, an lvalue reference to the
|
|
|
|
| 874 |
``` cpp
|
| 875 |
void f(const int*);
|
| 876 |
void g() {
|
| 877 |
const int N = 10;
|
| 878 |
[=] {
|
| 879 |
+
int arr[N]; // OK, not an odr-use, refers to variable with automatic storage duration
|
| 880 |
f(&N); // OK, causes N to be captured; &N points to
|
| 881 |
// the corresponding member of the closure type
|
| 882 |
};
|
| 883 |
}
|
| 884 |
```
|