|
625 | 625 | identifier\br
|
626 | 626 | \terminal{\&} identifier\br
|
627 | 627 | \terminal{this}
|
| 628 | + \terminal{* this} |
628 | 629 | \end{bnf}
|
629 | 630 |
|
630 | 631 | \begin{bnf}
|
|
939 | 940 | by \tcode{\&}. If a \grammarterm{lambda-capture} includes a
|
940 | 941 | \grammarterm{capture-default} that is \tcode{=}, each
|
941 | 942 | \grammarterm{simple-capture} of that \grammarterm{lambda-capture} shall
|
942 |
| -be of the form ``\tcode{\&} \grammarterm{identifier}''. Ignoring appearances in |
| 943 | +be of the form ``\tcode{\&} \grammarterm{identifier}'' or ``\tcode{* this}''. |
| 944 | +\enternote The form \tcode{[\&,this]} is redundant but accepted |
| 945 | +for compatibility with ISO \Cpp14. \exitnote |
| 946 | +Ignoring appearances in |
943 | 947 | \grammarterm{initializer}{s} of \grammarterm{init-capture}{s}, an identifier or
|
944 | 948 | \tcode{this} shall not appear more than once in a
|
945 | 949 | \grammarterm{lambda-capture}. \enterexample
|
946 | 950 |
|
947 | 951 | \begin{codeblock}
|
948 | 952 | struct S2 { void f(int i); };
|
949 | 953 | void S2::f(int i) {
|
950 |
| - [&, i]{ }; // OK |
951 |
| - [&, &i]{ }; // error: \tcode{i} preceded by \tcode{\&} when \tcode{\&} is the default |
952 |
| - [=, this]{ }; // error: \tcode{this} when \tcode{=} is the default |
953 |
| - [i, i]{ }; // error: \tcode{i} repeated |
| 954 | + [&, i]{ }; // OK |
| 955 | + [&, &i]{ }; // error: \tcode{i} preceded by \tcode{\&} when \tcode{\&} is the default |
| 956 | + [=, *this]{ }; // OK |
| 957 | + [=, this]{ }; // error: \tcode{this} when \tcode{=} is the default |
| 958 | + [i, i]{ }; // error: \tcode{i} repeated |
| 959 | + [this, *this]{ }; // error: \tcode{this} appears twice |
954 | 960 | }
|
955 | 961 | \end{codeblock}
|
956 | 962 | \exitexample
|
|
970 | 976 | usual rules for unqualified name lookup~(\ref{basic.lookup.unqual}); each such lookup
|
971 | 977 | shall find an entity. An entity that is designated by a
|
972 | 978 | \grammarterm{simple-capture}
|
973 |
| -is said to be \defn{explicitly captured}, and shall be \tcode{this} or |
| 979 | +is said to be \defn{explicitly captured}, and shall be \tcode{*this} |
| 980 | +(when the \grammarterm{simple-capture} |
| 981 | +is ``\tcode{this}'' or ``\tcode{* this}'') or |
974 | 982 | a variable with automatic storage duration declared in
|
975 | 983 | the reaching scope of the local lambda expression.
|
976 | 984 |
|
|
1005 | 1013 |
|
1006 | 1014 | \pnum
|
1007 | 1015 | A \grammarterm{lambda-expression} with an associated
|
1008 |
| -\grammarterm{capture-default} that does not explicitly capture \tcode{this} or |
| 1016 | +\grammarterm{capture-default} that does not explicitly capture \tcode{*this} or |
1009 | 1017 | a variable with automatic storage duration (this excludes any \grammarterm{id-expression}
|
1010 | 1018 | that has been found to refer to an \grammarterm{init-capture}{'s} associated
|
1011 | 1019 | \indextext{implicit capture!definition of}%
|
1012 | 1020 | non-static data member), is said to \defnx{implicitly capture}{capture!implicit}
|
1013 | 1021 | the entity (i.e.,
|
1014 |
| -\tcode{this} or a variable) if the \grammarterm{compound-statement}: |
| 1022 | +\tcode{*this} or a variable) if the \grammarterm{compound-statement}: |
1015 | 1023 | \begin{itemize}
|
1016 |
| -\item odr-uses~(\ref{basic.def.odr}) the entity, or |
| 1024 | +\item odr-uses~(\ref{basic.def.odr}) the entity (in the case of a variable), |
| 1025 | +\item odr-uses~(\ref{basic.def.odr}) \tcode{this} |
| 1026 | +(in the case of the object designated by \tcode{*this}), or |
1017 | 1027 | \item names the entity in a potentially-evaluated
|
1018 | 1028 | expression~(\ref{basic.def.odr}) where the enclosing full-expression depends on
|
1019 | 1029 | a generic lambda parameter declared within the reaching scope of the
|
|
1046 | 1056 | \pnum
|
1047 | 1057 | An entity is \defn{captured} if it is captured explicitly or implicitly. An entity
|
1048 | 1058 | captured by a \grammarterm{lambda-expression} is odr-used~(\ref{basic.def.odr}) in the scope
|
1049 |
| -containing the \grammarterm{lambda-expression}. If \tcode{this} is captured by a local |
| 1059 | +containing the \grammarterm{lambda-expression}. If \tcode{*this} is captured by a local |
1050 | 1060 | lambda expression, its nearest enclosing function shall be a non-static member function.
|
1051 | 1061 | If a \grammarterm{lambda-expression} or an instantiation of the function call
|
1052 | 1062 | operator template of a generic lambda odr-uses~(\ref{basic.def.odr}) \tcode{this} or a
|
|
1085 | 1095 | }
|
1086 | 1096 | };
|
1087 | 1097 | }
|
| 1098 | + |
| 1099 | +struct s2 { |
| 1100 | + double ohseven = .007; |
| 1101 | + auto f() { |
| 1102 | + return [this] { |
| 1103 | + return [*this] { |
| 1104 | + return ohseven; // OK |
| 1105 | + } |
| 1106 | + }(); |
| 1107 | + } |
| 1108 | + auto g() { |
| 1109 | + return [] { |
| 1110 | + return [*this] { }; // error: \tcode{*this} not captured by outer \grammarterm{lambda-expression} |
| 1111 | + }(); |
| 1112 | + } |
| 1113 | +}; |
1088 | 1114 | \end{codeblock}
|
1089 | 1115 | \exitexample
|
1090 | 1116 |
|
|
1105 | 1131 | \exitexample
|
1106 | 1132 |
|
1107 | 1133 | \pnum
|
1108 |
| -An entity is \defnx{captured by copy}{captured!by~copy} if it is implicitly captured and the |
1109 |
| -\grammarterm{capture-default} is \tcode{=} or if it is explicitly captured with a |
1110 |
| -capture that is not of the form \tcode{\&} \grammarterm{identifier} or |
| 1134 | +An entity is \defnx{captured by copy}{captured!by~copy} if |
| 1135 | +\begin{itemize} |
| 1136 | +\item |
| 1137 | +it is implicitly captured, |
| 1138 | +the \grammarterm{capture-default} is \tcode{=}, and |
| 1139 | +the captured entity is not \tcode{*this}, or |
| 1140 | +\item |
| 1141 | +it is explicitly captured with a capture that is not of the form |
| 1142 | +\tcode{this}, |
| 1143 | +\tcode{\&} \grammarterm{identifier}, or |
1111 | 1144 | \tcode{\&} \grammarterm{identifier} \grammarterm{initializer}.
|
| 1145 | +\end{itemize} |
1112 | 1146 | For each entity captured by copy, an
|
1113 | 1147 | unnamed non-static data member is declared in the closure type. The declaration order of
|
1114 | 1148 | these members is unspecified. The type of such a data member is
|
|
1117 | 1151 | the type of the corresponding captured entity otherwise.
|
1118 | 1152 | A member of an anonymous union shall not be captured by copy.
|
1119 | 1153 |
|
| 1154 | +\pnum |
| 1155 | +Every \grammarterm{id-expression} within the \grammarterm{compound-statement} of a |
| 1156 | +\grammarterm{lambda-expression} that is an odr-use~(\ref{basic.def.odr}) of an |
| 1157 | +entity captured by copy is transformed into an access to the corresponding unnamed data |
| 1158 | +member of the closure type. |
| 1159 | +\enternote An \grammarterm{id-expression} that is not an odr-use refers to |
| 1160 | +the original entity, never to a member of the closure type. Furthermore, such |
| 1161 | +an \grammarterm{id-expression} does not cause the implicit capture of the |
| 1162 | +entity. \exitnote |
| 1163 | +If \tcode{*this} is captured by copy, each odr-use of \tcode{this} is |
| 1164 | +transformed into a pointer to the corresponding unnamed data member of the closure type, |
| 1165 | +cast~(\ref{expr.cast}) to the type of \tcode{this}. \enternote The cast ensures that the |
| 1166 | +transformed expression is a prvalue. \exitnote \enterexample |
| 1167 | +\begin{codeblock} |
| 1168 | +void f(const int*); |
| 1169 | +void g() { |
| 1170 | + const int N = 10; |
| 1171 | + [=] { |
| 1172 | + int arr[N]; // OK: not an odr-use, refers to automatic variable |
| 1173 | + f(&N); // OK: causes \tcode{N} to be captured; \tcode{\&N} points to the |
| 1174 | + // corresponding member of the closure type |
| 1175 | + }; |
| 1176 | +} |
| 1177 | +\end{codeblock} |
| 1178 | +\exitexample |
| 1179 | + |
1120 | 1180 | \pnum
|
1121 | 1181 | An entity is \defnx{captured by reference}{captured!by~reference} if it is implicitly or explicitly
|
1122 | 1182 | captured but not captured by copy. It is unspecified whether additional unnamed
|
|
1164 | 1224 | \end{codeblock}
|
1165 | 1225 | \exitexample
|
1166 | 1226 |
|
1167 |
| - |
1168 |
| -\pnum |
1169 |
| -Every \grammarterm{id-expression} within the \grammarterm{compound-statement} of a |
1170 |
| -\grammarterm{lambda-expression} that is an odr-use~(\ref{basic.def.odr}) of an |
1171 |
| -entity captured by copy is transformed into an access to the corresponding unnamed data |
1172 |
| -member of the closure type. |
1173 |
| -\enternote An \grammarterm{id-expression} that is not an odr-use refers to |
1174 |
| -the original entity, never to a member of the closure type. Furthermore, such |
1175 |
| -an \grammarterm{id-expression} does not cause the implicit capture of the |
1176 |
| -entity. \exitnote |
1177 |
| -If \tcode{this} is captured, each odr-use of \tcode{this} is |
1178 |
| -transformed into an access to the corresponding unnamed data member of the closure type, |
1179 |
| -cast~(\ref{expr.cast}) to the type of \tcode{this}. \enternote The cast ensures that the |
1180 |
| -transformed expression is a prvalue. \exitnote \enterexample |
1181 |
| -\begin{codeblock} |
1182 |
| -void f(const int*); |
1183 |
| -void g() { |
1184 |
| - const int N = 10; |
1185 |
| - [=] { |
1186 |
| - int arr[N]; // OK: not an odr-use, refers to automatic variable |
1187 |
| - f(&N); // OK: causes \tcode{N} to be captured; \tcode{\&N} points to the |
1188 |
| - // corresponding member of the closure type |
1189 |
| - }; |
1190 |
| -} |
1191 |
| -\end{codeblock} |
1192 |
| -\exitexample |
1193 |
| - |
1194 | 1227 | \pnum
|
1195 | 1228 | Every occurrence of \tcode{decltype((x))} where \tcode{x} is a possibly
|
1196 | 1229 | parenthesized \grammarterm{id-expression} that names an entity of automatic storage
|
|
0 commit comments