Skip to content

Commit 6e754f3

Browse files
jensmaurertkoeppe
authored andcommitted
P2579R0 Mitigation strategies for P2036 "Changing scope for lambda trailing-return-type"
1 parent 9949fec commit 6e754f3

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

source/basic.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,12 @@
12861286
its scope extends to the end of the \grammarterm{deduction-guide}.
12871287
\end{itemize}
12881288

1289+
\rSec2[basic.scope.lambda]{Lambda scope}
1290+
1291+
A \grammarterm{lambda-expression} \tcode{E} introduces a \defnadj{lambda}{scope}
1292+
that starts immediately after the \grammarterm{lambda-introducer} of \tcode{E}
1293+
and extends to the end of the \grammarterm{compound-statement} of \tcode{E}.
1294+
12891295
\rSec2[basic.scope.namespace]{Namespace scope}
12901296

12911297
\pnum

source/expressions.tex

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,10 +1473,11 @@
14731473
If naming the entity from outside of an unevaluated operand within $S$
14741474
would refer to an entity
14751475
captured by copy in some intervening \grammarterm{lambda-expression},
1476-
then let $E$ be the innermost such \grammarterm{lambda-expression}, and:
1476+
then let $E$ be the innermost such \grammarterm{lambda-expression}.
14771477
\begin{itemize}
14781478
\item
1479-
If $P$ is in $E$'s function parameter scope
1479+
If there is such a \grammarterm{lambda-expression} and
1480+
if $P$ is in $E$'s function parameter scope
14801481
but not its \grammarterm{parameter-declaration-clause}, then
14811482
the type of the expression is
14821483
the type of a class member access expression\iref{expr.ref}
@@ -1488,11 +1489,11 @@
14881489
the type of such an identifier will typically be \keyword{const} qualified.
14891490
\end{note}
14901491
\item
1491-
Otherwise (if $P$ either precedes $E$'s function parameter scope or
1492+
Otherwise (if there is no such \grammarterm{lambda-expression} or
1493+
if $P$ either precedes $E$'s function parameter scope or
14921494
is in $E$'s \grammarterm{parameter-declaration-clause}),
1493-
the program is ill-formed.
1495+
the type of the expression is the type of the result.
14941496
\end{itemize}
1495-
Otherwise, the type of the expression is the type of the result.
14961497
\begin{note}
14971498
If the entity is a template parameter object for
14981499
a template parameter of type \tcode{T}\iref{temp.param},
@@ -1521,15 +1522,14 @@
15211522
return y2;
15221523
};
15231524

1524-
[=]<decltype(x) P>{}; // error: \tcode{x} refers to local entity but precedes the
1525-
// lambda's function parameter scope
1526-
[=](decltype((x)) y){}; // error: \tcode{x} refers to local entity but is in the lambda's
1527-
// \grammarterm{parameter-declaration-clause}
1525+
[=](decltype((x)) y){
1526+
decltype((x)) z = x; // OK, \tcode{y} has type \tcode{float\&}, \tcode{z} has type \tcode{float const\&}
1527+
};
15281528
[=]{
1529-
[]<decltype(x) P>{}; // OK, \tcode{x} is in the outer lambda's function parameter scope
15301529
[](decltype((x)) y){}; // OK, lambda takes a parameter of type \tcode{float const\&}
1531-
[x=1](decltype((x)) z){}; // error: \tcode{x} refers to \grammarterm{init-capture} but is in the lambda's
1532-
// \grammarterm{parameter-declaration-clause}
1530+
[x=1](decltype((x)) z){
1531+
decltype((x)) z = x; // OK, \tcode{y} has type \tcode{int\&}, \tcode{z} has type \tcode{int const\&}
1532+
};
15331533
};
15341534
}
15351535
\end{codeblock}
@@ -2310,22 +2310,26 @@
23102310
is said to be \defn{explicitly captured}.
23112311

23122312
\pnum
2313-
If an \grammarterm{identifier} in a \grammarterm{simple-capture} appears
2313+
If an \grammarterm{identifier} in a \grammarterm{capture} appears
23142314
as the \grammarterm{declarator-id} of a parameter of
2315-
the \grammarterm{lambda-declarator}{'s} \grammarterm{parameter-declaration-clause},
2315+
the \grammarterm{lambda-declarator}'s \grammarterm{parameter-declaration-clause}
2316+
or as the name of a template parameter of
2317+
the \grammarterm{lambda-expression}'s \grammarterm{template-parameter-list},
23162318
the program is ill-formed.
23172319
\begin{example}
23182320
\begin{codeblock}
23192321
void f() {
23202322
int x = 0;
2321-
auto g = [x](int x) { return 0; }; // error: parameter and \grammarterm{simple-capture} have the same name
2323+
auto g = [x](int x) { return 0; }; // error: parameter and \grammarterm{capture} have the same name
2324+
auto h = [y = 0]<typename y>(y) { return 0; }; // error: template parameter and \grammarterm{capture}
2325+
// have the same name
23222326
}
23232327
\end{codeblock}
23242328
\end{example}
23252329

23262330
\pnum
2327-
An \grammarterm{init-capture} inhabits the function parameter scope of
2328-
the \grammarterm{lambda-expression}'s \grammarterm{parameter-declaration-clause}.
2331+
An \grammarterm{init-capture} inhabits
2332+
the lambda scope of the \grammarterm{lambda-expression}.
23292333
An \grammarterm{init-capture} without ellipsis
23302334
behaves as if it declares and explicitly captures a variable of
23312335
the form ``\keyword{auto} \grammarterm{init-capture} \tcode{;}'', except that:

0 commit comments

Comments
 (0)