forked from cplusplus/draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressions.tex
More file actions
4909 lines (4335 loc) · 196 KB
/
expressions.tex
File metadata and controls
4909 lines (4335 loc) · 196 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%!TEX root = std.tex
\rSec0[expr]{Expressions}
%gram: \rSec1[gram.expr]{Expressions}
%gram:
\indextext{\idxcode{operator new}|seealso{\tcode{new}}}%
\indextext{\idxcode{operator delete}|seealso{\tcode{delete}}}%
\indextext{usual arithmetic conversions|see{conversion, usual arithmetic}}%
\indextext{\idxcode{==}|see{equality~operator}}%
\indextext{\idxcode{"!=}|see{inequality~operator}}
\indextext{\idxcode{static_cast}|see{cast, static}}%
\indextext{\idxcode{dynamic_cast}|see{cast, dynamic}}%
\indextext{\idxcode{const_cast}|see{cast, const}}%
\indextext{\idxcode{reinterpret_cast}|see{cast, reinterpret}}
\pnum
\indextext{expression|(}%
\enternote
Clause~\ref{expr} defines the syntax, order of evaluation, and meaning
of expressions.\footnote{The precedence of operators is not directly specified, but it can be
derived from the syntax.}
An expression is a sequence of operators and operands that specifies a
computation. An expression can result in a value and can cause side
effects.
\exitnote
\pnum
\indextext{operator!overloaded}%
\enternote
Operators can be overloaded, that is, given meaning when applied to
expressions of class type~(Clause \ref{class}) or enumeration
type~(\ref{dcl.enum}). Uses of overloaded operators are transformed into
function calls as described in~\ref{over.oper}. Overloaded operators
obey the rules for syntax specified in Clause~\ref{expr}, but the
requirements of operand type, value category, and evaluation order are replaced
by the rules for function call. Relations between operators, such as
\tcode{++a} meaning \tcode{a+=1}, are not guaranteed for overloaded
operators~(\ref{over.oper}), and are not guaranteed for operands of type
\tcode{bool}.
\exitnote
\pnum
Clause~\ref{expr} defines the effects of operators when applied to types
for which they have not been overloaded. Operator overloading shall not
modify the rules for the \term{built-in operators}, that
is, for operators applied to types for which they are defined by this
Standard. However, these built-in operators participate in overload
resolution, and as part of that process user-defined conversions will be
considered where necessary to convert the operands to types appropriate
for the built-in operator. If a built-in operator is selected, such
conversions will be applied to the operands before the operation is
considered further according to the rules in Clause~\ref{expr};
see~\ref{over.match.oper},~\ref{over.built}.
\pnum
\indextext{exception!arithmetic}%
\indextext{exception!undefined arithmetic}%
\indextext{overflow!undefined}%
\indextext{zero!division by undefined}%
\indextext{zero!remainder undefined}%
If during the evaluation of an expression, the result is not
mathematically defined or not in the range of representable values for
its type, the behavior is undefined.
\enternote
\indextext{overflow}%
most existing implementations of \Cpp ignore integer overflows.
Treatment of division by zero, forming a remainder using a zero divisor,
and all floating point exceptions vary among machines, and is usually
adjustable by a library function.
\exitnote
\pnum
\indextext{expression!reference}%
If an expression initially has the type ``reference to
\tcode{T}''~(\ref{dcl.ref},~\ref{dcl.init.ref}), the type is adjusted to
\tcode{T} prior to any further analysis. The expression designates the
object or function denoted by the reference, and the expression
is an lvalue or an xvalue, depending on the expression.
\pnum
If a prvalue initially has the type ``\cv{} \tcode{T},'' where
\tcode{T} is a cv-unqualified non-class, non-array type, the type of
the expression is adjusted to \tcode{T} prior to any further analysis.
\pnum
\indextext{expression!rvalue~reference}%
\enternote
An expression is an xvalue if it is:
\begin{itemize}
\item the result of calling a function, whether implicitly or explicitly,
whose return type is an rvalue reference to object type,
\item a cast to an rvalue reference to object type,
\item a class member access expression designating a non-static data member
of non-reference type
in which the object expression is an xvalue, or
\item a \tcode{.*} pointer-to-member expression in which the first operand is
an xvalue and the second operand is a pointer to data member.
\end{itemize}
In general, the effect of this rule is that named rvalue references are
treated as lvalues and unnamed rvalue references to objects are treated as
xvalues; rvalue references to functions are treated as lvalues whether named or not.
\exitnote
\enterexample
\begin{codeblock}
struct A {
int m;
};
A&& operator+(A, A);
A&& f();
A a;
A&& ar = static_cast<A&&>(a);
\end{codeblock}
The expressions \tcode{f()}, \tcode{f().m}, \tcode{static_cast<A\&\&>(a)}, and \tcode{a + a}
are xvalues. The expression \tcode{ar} is an lvalue.
\exitexample
\pnum
In some contexts, \defnx{unevaluated operands}{unevaluated operand}
appear~(\ref{expr.typeid}, \ref{expr.sizeof}, \ref{expr.unary.noexcept}, \ref{dcl.type.simple}).
An unevaluated operand is not evaluated. An unevaluated operand is
considered a full-expression.
\enternote
In an unevaluated operand, a non-static class member may be
named~(\ref{expr.prim}) and naming of objects or functions does not, by
itself, require that a definition be provided~(\ref{basic.def.odr}).
\exitnote
\pnum
Whenever a glvalue expression appears as an operand of an operator that
expects a prvalue for that operand, the
lvalue-to-rvalue~(\ref{conv.lval}), array-to-pointer~(\ref{conv.array}),
or function-to-pointer~(\ref{conv.func}) standard conversions are
applied to convert the expression to a prvalue.
\enternote
because cv-qualifiers are removed from the type of an expression of
non-class type when the expression is converted to a prvalue, an lvalue
expression of type \tcode{const int} can, for example, be used where
a prvalue expression of type \tcode{int} is required.
\exitnote
\pnum
\indextext{conversion!usual arithmetic}%
Many binary operators that expect operands of arithmetic or enumeration
type cause conversions and yield result types in a similar way. The
purpose is to yield a common type, which is also the type of the result.
This pattern is called the \term{usual arithmetic conversions},
which are defined as follows:
\begin{itemize}
\item If either operand is of scoped enumeration type~(\ref{dcl.enum}), no conversions
are performed; if the other operand does not have the same type, the expression is
ill-formed.
\item If either operand is of type \tcode{long} \tcode{double}, the
other shall be converted to \tcode{long} \tcode{double}.
\item Otherwise, if either operand is \tcode{double}, the other shall be
converted to \tcode{double}.
\item Otherwise, if either operand is \tcode{float}, the other shall be
converted to \tcode{float}.
\item Otherwise, the integral promotions~(\ref{conv.prom}) shall be
performed on both operands.\footnote{As a consequence, operands of type \tcode{bool}, \tcode{char16_t},
\tcode{char32_t}, \tcode{wchar_t}, or an enumerated type are converted
to some integral type.}
Then the following rules shall be applied to the promoted operands:
\begin{itemize}
\item If both operands have the same type, no further conversion is
needed.
\item Otherwise, if both operands have signed integer types or both have
unsigned integer types, the operand with the type of lesser integer
conversion rank shall be converted to the type of the operand with
greater rank.
\item Otherwise, if the operand that has unsigned integer type has rank
greater than or equal to the rank of the type of the other operand, the
operand with signed integer type shall be converted to the type of the
operand with unsigned integer type.
\item Otherwise, if the type of the operand with signed integer type can
represent all of the values of the type of the operand with unsigned
integer type, the operand with unsigned integer type shall be converted
to the type of the operand with signed integer type.
\item Otherwise, both operands shall be converted to the unsigned
integer type corresponding to the type of the operand with signed
integer type.
\end{itemize}
\end{itemize}
\pnum
In some contexts, an expression only appears for its side effects. Such an
expression is called a \defn{discarded-value expression}. The expression is
evaluated and its value is discarded. The array-to-pointer~(\ref{conv.array})
and function-to-pointer~(\ref{conv.func}) standard conversions are not
applied. The lvalue-to-rvalue conversion~(\ref{conv.lval}) is applied
if and only if
the expression is a glvalue of volatile-qualified type and it is one of the
following:
\begin{itemize}
\item \tcode{(} \grammarterm{expression} \tcode{)}, where
\grammarterm{expression} is one of these expressions,
\item \grammarterm{id-expression}~(\ref{expr.prim.general}),
\item subscripting~(\ref{expr.sub}),
\item class member access~(\ref{expr.ref}),
\item indirection~(\ref{expr.unary.op}),
\item pointer-to-member operation~(\ref{expr.mptr.oper}),
\item conditional expression~(\ref{expr.cond}) where both the second and the
third operands are one of these expressions, or
\item comma expression~(\ref{expr.comma}) where the right operand is one of
these expressions.
\end{itemize}
\enternote Using an overloaded operator causes a function call; the
above covers only operators with built-in meaning. If the lvalue is of
class type, it must have a volatile copy constructor to initialize the
temporary that is the result of the lvalue-to-rvalue
conversion. \exitnote
\pnum
The values of the floating operands and the results of floating
expressions may be represented in greater precision and range than that
required by the type; the types are not changed\
thereby.\footnote{The cast and assignment operators must still perform their specific
conversions as described in~\ref{expr.cast},~\ref{expr.static.cast}
and~\ref{expr.ass}.}
\pnum
The \term{cv-combined type} of two types \tcode{T1} and \tcode{T2}
is a type \tcode{T3}
similar to \tcode{T1} whose cv-qualification signature~(\ref{conv.qual}) is:
\begin{itemize}
\item
for every $j > 0$, \cv$_{3,j}$ is the union of
\cv$_{1,j}$ and \cv$_{2,j}$;
\item
if the resulting \cv$_{3,j}$ is different from
\cv$_{1,j}$ or \cv$_{2,j}$, then
\tcode{const} is added to every \cv$_{3,k}$ for $0 < k < j$.
\end{itemize}
\enternote Given similar types \tcode{T1} and \tcode{T2}, this
construction ensures that
both can be converted to \tcode{T3}. \exitnote
The \term{composite pointer type} of
two operands \tcode{p1} and
\tcode{p2} having types \tcode{T1} and \tcode{T2}, respectively, where at least one is a
pointer or pointer to member type or
\tcode{std::nullptr_t}, is:
\begin{itemize}
\item
if both \tcode{p1} and \tcode{p2} are null pointer constants,
\tcode{std::nullptr_t};
\item
if either \tcode{p1} or \tcode{p2} is a null pointer constant, \tcode{T2} or \tcode{T1},
respectively;
\item
if \tcode{T1} or \tcode{T2} is ``pointer to \cvqual{cv1} \tcode{void}'' and the
other type is ``pointer to \cvqual{cv2} T'', ``pointer to \cvqual{cv12}
\tcode{void}'', where \cvqual{cv12} is the union of \cvqual{cv1}
and \cvqual{cv2};
\item
if \tcode{T1} is ``pointer to \cvqual{cv1} \tcode{C1}'' and \tcode{T2} is ``pointer to
\cvqual{cv2} \tcode{C2}'', where \tcode{C1} is reference-related to \tcode{C2} or \tcode{C2} is
reference-related to \tcode{C1}~(\ref{dcl.init.ref}), the cv-combined type
of \tcode{T1} and \tcode{T2} or the cv-combined type of \tcode{T2} and \tcode{T1},
respectively;
\item
if \tcode{T1} is ``pointer to member of \tcode{C1} of type \cvqual{cv1} \tcode{U1}'' and \tcode{T2} is
``pointer to member of \tcode{C2} of type \cvqual{cv2} \tcode{U2}'' where \tcode{C1} is
reference-related to \tcode{C2} or \tcode{C2} is reference-related to
\tcode{C1}~(\ref{dcl.init.ref}), the cv-combined type of \tcode{T2} and \tcode{T1} or the cv-combined type
of \tcode{T1} and \tcode{T2}, respectively;
\item
if \tcode{T1} and \tcode{T2} are similar types~(\ref{conv.qual}), the cv-combined type of \tcode{T1} and
\tcode{T2};
\item
otherwise, a program that necessitates the determination of a
composite pointer type is ill-formed.
\end{itemize}
\enterexample
\begin{codeblock}
typedef void *p;
typedef const int *q;
typedef int **pi;
typedef const int **pci;
\end{codeblock}
The composite pointer type of \tcode{p} and \tcode{q} is ``pointer to \tcode{const void}''; the
composite pointer type of \tcode{pi} and \tcode{pci} is ``pointer to \tcode{const} pointer to
\tcode{const int}''.
\exitexample
\rSec1[expr.prim]{Primary expressions}%
\indextext{expression!primary|(}
\rSec2[expr.prim.general]{General}
\begin{bnf}
\nontermdef{primary-expression}\br
literal\br
\terminal{this}\br
\terminal{(} expression \terminal{)}\br
id-expression\br
lambda-expression\br
fold-expression
\end{bnf}
\begin{bnf}
\nontermdef{id-expression}\br
unqualified-id\br
qualified-id
\end{bnf}
\begin{bnf}
\nontermdef{unqualified-id}\br
identifier\br
operator-function-id\br
conversion-function-id\br
literal-operator-id\br
\terminal{\tilde} class-name\br
\terminal{\tilde} decltype-specifier\br
template-id
\end{bnf}
\pnum
A
\indextext{literal}%
\indextext{constant}%
\grammarterm{literal}
is a primary expression.
Its type depends on its form~(\ref{lex.literal}).
A string literal is an lvalue; all other literals are prvalues.
\pnum
\indextext{\idxcode{this}}%
The keyword \tcode{this} names a pointer to the object for which a non-static member
function~(\ref{class.this}) is invoked or a non-static data member's
initializer~(\ref{class.mem}) is evaluated.
\pnum
If a declaration declares a member function or member function template of a
class \tcode{X}, the expression \tcode{this} is a prvalue of type ``pointer to
\grammarterm{cv-qualifier-seq} \tcode{X}'' between the optional
\grammarterm{cv-qualifer-seq} and the end of the \grammarterm{function-definition},
\grammarterm{member-declarator}, or \grammarterm{declarator}. It shall not appear
before the optional \grammarterm{cv-qualifier-seq} and it shall not appear within
the declaration of a static member function (although its type and value category
are defined within a static member function as they are within a non-static
member function). \enternote this is because declaration matching does not
occur until the complete declarator is known. \exitnote Unlike the object
expression in other contexts, \tcode{*this} is not required to be of complete
type for purposes of class member access~(\ref{expr.ref}) outside the member
function body. \enternote only class members declared prior to the declaration
are visible. \exitnote
\enterexample
\begin{codeblock}
struct A {
char g();
template<class T> auto f(T t) -> decltype(t + g())
{ return t + g(); }
};
template auto A::f(int t) -> decltype(t + g());
\end{codeblock}
\exitexample
\pnum
Otherwise, if a \grammarterm{member-declarator} declares a non-static data
member~(\ref{class.mem}) of a class \tcode{X}, the expression \tcode{this} is
a prvalue of type ``pointer to \tcode{X}'' within the
optional \grammarterm{brace-or-equal-initializer}. It shall not appear elsewhere
in the \grammarterm{member-declarator}.
\pnum
The expression \tcode{this} shall not appear in any other context.
\enterexample
\begin{codeblock}
class Outer {
int a[sizeof(*this)]; // error: not inside a member function
unsigned int sz = sizeof(*this); // OK: in \grammarterm{brace-or-equal-initializer}
void f() {
int b[sizeof(*this)]; // OK
struct Inner {
int c[sizeof(*this)]; // error: not inside a member function of \tcode{Inner}
};
}
};
\end{codeblock}
\exitexample
\pnum
\indextext{expression!parenthesized}%
A parenthesized expression is a primary expression whose type and value
are identical to those of the enclosed expression. The presence of
parentheses does not affect whether the expression is an lvalue. The
parenthesized expression can be used in exactly the same contexts as
those where the enclosed expression can be used, and with the same
meaning, except as otherwise indicated.
\pnum
\indextext{name}%
\indextext{id-expression}%
An \grammarterm{id-expression} is a restricted form of a
\grammarterm{primary-expression}.
\enternote
an \grammarterm{id-expression} can appear after \tcode{.} and \tcode{->}
operators~(\ref{expr.ref}).
\exitnote
\pnum
\indextext{identifier}%
An \grammarterm{identifier} is an \grammarterm{id-expression} provided it has
been suitably declared (Clause~\ref{dcl.dcl}).
\enternote
for \grammarterm{operator-function-id}{s}, see~\ref{over.oper}; for
\grammarterm{conversion-function-id}{s}, see~\ref{class.conv.fct}; for
\grammarterm{literal-operator-id}{s}, see~\ref{over.literal}; for
\grammarterm{template-id}{s}, see~\ref{temp.names}. A \grammarterm{class-name}
or \grammarterm{decltype-specifier}
prefixed by \tcode{\tilde} denotes a destructor; see~\ref{class.dtor}.
Within the definition of a non-static member function, an
\grammarterm{identifier} that names a non-static member is transformed to a
class member access expression~(\ref{class.mfct.non-static}).
\exitnote
The type of the expression is the type of the \grammarterm{identifier}. The
result is the entity denoted by the identifier. The result is an lvalue
if the entity is a function, variable, or data member and a prvalue otherwise.
\clearpage
\indextext{operator!scope~resolution}%
\indextext{\idxcode{::}|see{scope~resolution~operator}}%
%
\begin{bnf}
\nontermdef{qualified-id}\br
nested-name-specifier \terminal{template}\opt unqualified-id
\end{bnf}
\indextext{operator!scope~resolution}%
\indextext{name~hiding}%
%
\begin{bnf}
\nontermdef{nested-name-specifier}\br
\terminal{::}\br
type-name \terminal{::}\br
namespace-name \terminal{::}\br
decltype-specifier \terminal{::}\br
nested-name-specifier identifier \terminal{::}\br
nested-name-specifier \terminal{template}\opt simple-template-id \terminal{::}
\end{bnf}
The type denoted by a \grammarterm{decltype-specifier} in a
\grammarterm{nested-name-specifier} shall be a class or enumeration
type.
\pnum
A \grammarterm{nested-name-specifier} that denotes a class, optionally
followed by the keyword \tcode{template}~(\ref{temp.names}), and then
followed by the name of a member of either that class~(\ref{class.mem})
or one of its base classes (Clause~\ref{class.derived}), is a
\indextext{id!qualified}%
\grammarterm{qualified-id};~\ref{class.qual} describes name lookup for
class members that appear in \grammarterm{qualified-ids}. The result is the
member. The type of the result is the type of the member. The result is
an lvalue if the member is a static member function or a data member and a
prvalue otherwise.
\enternote
a class member can be referred to using a \grammarterm{qualified-id} at any
point in its potential scope~(\ref{basic.scope.class}).
\exitnote
Where
\grammarterm{class-name} \tcode{::\tilde}~\grammarterm{class-name} is used,
the two \grammarterm{class-name}{s} shall refer to the same class; this
notation names the destructor~(\ref{class.dtor}).
The form \tcode{\tilde}~\grammarterm{decltype-specifier} also denotes the destructor,
but it shall not be used as the \grammarterm{unqualified-id} in a \grammarterm{qualified-id}.
\enternote
a \grammarterm{typedef-name} that names a class is a
\grammarterm{class-name}~(\ref{class.name}).
\exitnote
\pnum
The \grammarterm{nested-name-specifier} \tcode{::} names the global namespace.
A \grammarterm{nested-name-specifier} that names a
namespace~(\ref{basic.namespace}), followed by the name of a member of
that namespace (or the name of a member of a namespace made visible by a
\grammarterm{using-directive}), is a
\indextext{id!qualified}%
\grammarterm{qualified-id};~\ref{namespace.qual} describes name lookup for
namespace members that appear in \grammarterm{qualified-ids}. The result is
the member. The type of the result is the type of the member. The result
is an lvalue if the member is a function or a variable and a prvalue otherwise.
\pnum
A \grammarterm{nested-name-specifier} that denotes an
enumeration~(\ref{dcl.enum}), followed by the name of an
enumerator of that enumeration, is a \grammarterm{qualified-id}
that refers to the enumerator. The result is the enumerator. The type
of the result is the type of the enumeration. The result is a prvalue.
\pnum
In a \grammarterm{qualified-id}, if the
\grammarterm{unqualified-id}
is a
\grammarterm{conversion-function-id}, its \grammarterm{conversion-type-id}
shall denote the same type in both the context in which the entire
\grammarterm{qualified-id} occurs and in the context of the class denoted
by the \grammarterm{nested-name-specifier}.
\pnum
An \grammarterm{id-expression} that denotes a non-static data member or
non-static member function of a class can only be used:
\begin{itemize}
\item as part of a class member access~(\ref{expr.ref}) in which the
object expression
refers to the member's class\footnote{This also applies when the object expression
is an implicit \tcode{(*this)}~(\ref{class.mfct.non-static}).} or a class derived from
that class, or
\item to form a pointer to member~(\ref{expr.unary.op}), or
\item if that \grammarterm{id-expression} denotes a non-static data member
and it appears in an unevaluated operand.
\enterexample
\begin{codeblock}
struct S {
int m;
};
int i = sizeof(S::m); // OK
int j = sizeof(S::m + 42); // OK
\end{codeblock}
\exitexample
\end{itemize}
\rSec2[expr.prim.lambda]{Lambda expressions}%
\indextext{expression!lambda|(}
\pnum
Lambda expressions provide a concise way to create simple function objects.
\enterexample
\begin{codeblock}
#include <algorithm>
#include <cmath>
void abssort(float* x, unsigned N) {
std::sort(x, x + N,
[](float a, float b) {
return std::abs(a) < std::abs(b);
});
}
\end{codeblock}
\exitexample
\begin{bnf}
\nontermdef{lambda-expression}\br
lambda-introducer lambda-declarator\opt compound-statement
\end{bnf}
\begin{bnf}
\nontermdef{lambda-introducer}\br
\terminal{[} lambda-capture\opt \terminal{]}
\end{bnf}
\begin{bnf}
\nontermdef{lambda-capture}\br
capture-default\br
capture-list\br
capture-default \terminal{,} capture-list
\end{bnf}
\begin{bnf}
\nontermdef{capture-default}\br
\terminal{\&}\br
\terminal{=}
\end{bnf}
\begin{bnf}
\nontermdef{capture-list}\br
capture \terminal{...\opt}\br
capture-list \terminal{,} capture \terminal{...\opt}
\end{bnf}
\begin{bnf}
\nontermdef{capture}\br
simple-capture\br
init-capture
\end{bnf}
\begin{bnf}
\nontermdef{simple-capture}\br
identifier\br
\terminal{\&} identifier\br
\terminal{this}
\end{bnf}
\begin{bnf}
\nontermdef{init-capture}\br
identifier initializer\br
\terminal{\&} identifier initializer
\end{bnf}
\begin{bnf}
\nontermdef{lambda-declarator}\br
\terminal{(} parameter-declaration-clause \terminal{)} \terminal{mutable}\opt\br
\hspace*{\bnfindentinc}exception-specification\opt attribute-specifier-seq\opt trailing-return-type\opt
\end{bnf}
\pnum
The evaluation of a \grammarterm{lambda-expression} results in a prvalue
temporary~(\ref{class.temporary}). This temporary is called the \defn{closure object}. A
\grammarterm{lambda-expression} shall not appear in an unevaluated operand
(Clause~\ref{expr}), in a \grammarterm{template-argument},
in an \grammarterm{alias-declaration},
in a typedef declaration, or in the declaration of a function or function
template outside its function body and default arguments.
\enternote
The intention is to prevent lambdas from appearing in a signature.
\exitnote
\enternote
A closure object behaves like a function
object~(\ref{function.objects}).\exitnote
\pnum
The type of the \grammarterm{lambda-expression} (which is also the type of the
closure object) is a unique, unnamed non-union class type --- called the \defn{closure
type} --- whose properties are described below. This class type is neither an
aggregate~(\ref{dcl.init.aggr}) nor a literal type~(\ref{basic.types}).
The closure type is declared in the smallest block
scope, class scope, or namespace scope that contains the corresponding
\grammarterm{lambda-expression}. \enternote This determines the set of namespaces and
classes associated with the closure type~(\ref{basic.lookup.argdep}). The parameter
types of a \grammarterm{lambda-declarator} do not affect these associated namespaces and
classes. \exitnote An implementation may define the closure type differently from what
is described below provided this does not alter the observable behavior of the program
other than by changing:
\begin{itemize}
\item the size and/or alignment of the closure type,
\item whether the closure type is trivially copyable (Clause~\ref{class}),
\item whether the closure type is a standard-layout class (Clause~\ref{class}),
or
\item whether the closure type is a POD class (Clause~\ref{class}).
\end{itemize}
An implementation shall not add members of rvalue reference type to the closure
type.
\pnum
If a \grammarterm{lambda-expression} does not include a
\grammarterm{lambda-declarator}, it is as if the \grammarterm{lambda-declarator} were
\tcode{()}.
The lambda return type is \tcode{auto}, which is replaced by the
\grammarterm{trailing-return-type} if provided and/or deduced from
\tcode{return} statements as described in~\ref{dcl.spec.auto}.
\enterexample
\begin{codeblock}
auto x1 = [](int i){ return i; }; // OK: return type is \tcode{int}
auto x2 = []{ return { 1, 2 }; }; // error: deducing return type from \grammarterm{braced-init-list}
int j;
auto x3 = []()->auto&& { return j; }; // OK: return type is \tcode{int\&}
\end{codeblock}
\exitexample
\pnum
The closure type for a non-generic \grammarterm{lambda-expression} has a public
inline function call operator~(\ref{over.call}) whose parameters and return type
are described by the \grammarterm{lambda-expression}'s
\grammarterm{parameter-declaration-clause} and \grammarterm{trailing-return-type}
respectively.
For a generic lambda, the closure type has a public inline function call
operator member template~(\ref{temp.mem}) whose
\grammarterm{template-parameter-list} consists of one invented type
\grammarterm{template-parameter} for each occurrence of \tcode{auto} in the
lambda's \grammarterm{parameter-declaration-clause}, in order of appearance.
The invented type \grammarterm{template-parameter} is a parameter pack if
the corresponding \grammarterm{parameter-declaration} declares a function
parameter pack~(\ref{dcl.fct}). The return type and function parameters of the
function call operator template are derived from the
\grammarterm{lambda-expression}{'s} \grammarterm{trailing-return-type} and
\grammarterm{parameter-declaration-clause} by replacing each occurrence of
\tcode{auto} in the \grammarterm{decl-specifier}{s} of the
\grammarterm{parameter-declaration-clause} with the name of the corresponding
invented \grammarterm{template-parameter}.
\enterexample
\begin{codeblock}
auto glambda = [](auto a, auto&& b) { return a < b; };
bool b = glambda(3, 3.14); // OK
auto vglambda = [](auto printer) {
return [=](auto&& ... ts) { // OK: \tcode{ts} is a function parameter pack
printer(std::forward<decltype(ts)>(ts)...);
return [=]() {
printer(ts ...);
};
};
};
auto p = vglambda( [](auto v1, auto v2, auto v3)
{ std::cout << v1 << v2 << v3; } );
auto q = p(1, 'a', 3.14); // OK: outputs \tcode{1a3.14}
q(); // OK: outputs \tcode{1a3.14}
\end{codeblock}
\exitexample
This function call operator or operator template is declared
\tcode{const}~(\ref{class.mfct.non-static}) if and only if the
\grammarterm{lambda-expression}'s \grammarterm{parameter-declaration-clause} is not
followed by \tcode{mutable}. It is neither virtual nor declared \tcode{volatile}. Any
\grammarterm{exception-specification} specified on a \grammarterm{lambda-expression}
applies to the corresponding function call operator or operator template.
An \grammarterm{attribute-specifier-seq} in a \grammarterm{lambda-declarator} appertains
to the type of the corresponding function call operator or operator template.
\enternote Names referenced in
the \grammarterm{lambda-declarator} are looked up in the context in which the
\grammarterm{lambda-expression} appears. \exitnote
\pnum
The closure type for a non-generic \grammarterm{lambda-expression} with no
\grammarterm{lambda-capture}
has a public non-virtual non-explicit const conversion function to pointer to
function with \Cpp language linkage~(\ref{dcl.link}) having
the same parameter and return types as the closure type's function call operator. The
value returned by this conversion function shall be the address of a function that, when
invoked, has the same effect as invoking the closure type's function call operator.
For a generic lambda with no \grammarterm{lambda-capture}, the closure type has a
public non-virtual non-explicit const conversion function template to
pointer to function. The conversion function template has the same invented
\grammarterm{template-parameter-list}, and the pointer to function has the same
parameter types, as the function call operator template. The return type of
the pointer to function shall behave as if it were a
\grammarterm{decltype-specifier} denoting the return type of the corresponding
function call operator template specialization.
\enternote If the generic lambda has no \grammarterm{trailing-return-type} or
the \grammarterm{trailing-return-type} contains a placeholder type, return type
deduction of the corresponding function call operator template specialization
has to be done. The corresponding specialization is that instantiation of the
function call operator template with the same template arguments as those
deduced for the conversion function template. Consider the following:
\begin{codeblock}
auto glambda = [](auto a) { return a; };
int (*fp)(int) = glambda;
\end{codeblock}
The behavior of the conversion function of \tcode{glambda} above is like
that of the following conversion function:
\begin{codeblock}
struct Closure {
template<class T> auto operator()(T t) const { ... }
template<class T> static auto lambda_call_operator_invoker(T a) {
// forwards execution to \tcode{operator()(a)} and therefore has
// the same return type deduced
...
}
template<class T> using fptr_t =
decltype(lambda_call_operator_invoker(declval<T>())) (*)(T);
template<class T> operator fptr_t<T>() const
{ return &lambda_call_operator_invoker; }
};
\end{codeblock}
\exitnote
\enterexample
\begin{codeblock}
void f1(int (*)(int)) { }
void f2(char (*)(int)) { }
void g(int (*)(int)) { } // \#1
void g(char (*)(char)) { } // \#2
void h(int (*)(int)) { } // \#3
void h(char (*)(int)) { } // \#4
auto glambda = [](auto a) { return a; };
f1(glambda); // OK
f2(glambda); // error: ID is not convertible
g(glambda); // error: ambiguous
h(glambda); // OK: calls \#3 since it is convertible from ID
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
\end{codeblock}
\exitexample
The value returned by any given specialization of this conversion function
template shall be the address of a function that, when invoked, has the same
effect as invoking the generic lambda's corresponding function call operator
template specialization.
\enternote
This will result in the implicit instantiation of the generic lambda's body.
The instantiated generic lambda's return type and parameter types shall match
the return type and parameter types of the pointer to function.
\exitnote
\enterexample
\begin{codeblock}
auto GL = [](auto a) { std::cout << a; return a; };
int (*GL_int)(int) = GL; // OK: through conversion function template
GL_int(3); // OK: same as \tcode{GL(3)}
\end{codeblock}
\exitexample
\pnum
The \grammarterm{lambda-expression}'s \grammarterm{compound-statement} yields the
\grammarterm{function-body}~(\ref{dcl.fct.def}) of the function call operator, but for
purposes of name lookup~(\ref{basic.lookup}), determining the type and value of
\tcode{this}~(\ref{class.this}) and transforming \grammarterm{id-expression}{s}
referring to non-static class members into class member access expressions using
\tcode{(*this)}~(\ref{class.mfct.non-static}), the \grammarterm{compound-statement} is
considered in the context of the \grammarterm{lambda-expression}. \enterexample
\begin{codeblock}
struct S1 {
int x, y;
int operator()(int);
void f() {
[=]()->int {
return operator()(this->x + y); // equivalent to \tcode{S1::operator()(this->x + (*this).y)}
// \tcode{this} has type \tcode{S1*}
};
}
};
\end{codeblock}
\exitexample
Further, a variable \tcode{__func__} is implicitly defined at the beginning of
the \grammarterm{compound-statement} of the \grammarterm{lambda-expression},
with semantics as described in~\ref{dcl.fct.def.general}.
\pnum
If a \grammarterm{lambda-capture} includes a \grammarterm{capture-default} that
is \tcode{\&}, no identifier in a \grammarterm{simple-capture} of that
\grammarterm{lambda-capture} shall be preceded
by \tcode{\&}. If a \grammarterm{lambda-capture} includes a
\grammarterm{capture-default} that is \tcode{=}, each
\grammarterm{simple-capture} of that \grammarterm{lambda-capture} shall
be of the form ``\tcode{\&} \grammarterm{identifier}''. Ignoring appearances in
\grammarterm{initializer}{s} of \grammarterm{init-capture}{s}, an identifier or
\tcode{this} shall not appear more than once in a
\grammarterm{lambda-capture}. \enterexample
\begin{codeblock}
struct S2 { void f(int i); };
void S2::f(int i) {
[&, i]{ }; // OK
[&, &i]{ }; // error: \tcode{i} preceded by \tcode{\&} when \tcode{\&} is the default
[=, this]{ }; // error: \tcode{this} when \tcode{=} is the default
[i, i]{ }; // error: \tcode{i} repeated
}
\end{codeblock}
\exitexample
\pnum
A \grammarterm{lambda-expression} whose smallest enclosing scope is a block
scope~(\ref{basic.scope.block}) is a \defn{local lambda expression}; any other
\grammarterm{lambda-expression} shall not have a \grammarterm{capture-default} or
\grammarterm{simple-capture} in its
\grammarterm{lambda-introducer}. The \defn{reaching scope} of a local lambda expression
is the set of enclosing scopes up to and including the innermost enclosing function and
its parameters. \enternote This reaching scope includes any intervening
\grammarterm{lambda-expression}{s}. \exitnote
\pnum
The \grammarterm{identifier} in a \grammarterm{simple-capture} is looked up using the
usual rules for unqualified name lookup~(\ref{basic.lookup.unqual}); each such lookup
shall find an entity. An entity that is designated by a
\grammarterm{simple-capture}
is said to be \defn{explicitly captured}, and shall be \tcode{this} or
a variable with automatic storage duration declared in
the reaching scope of the local lambda expression.
\pnum
An \grammarterm{init-capture} behaves as if it declares and explicitly captures a
variable of
the form ``\tcode{auto} \grammarterm{init-capture} \tcode{;}''
whose declarative region is the \grammarterm{lambda-expression}'s
\grammarterm{compound-statement}, except that:
\begin{itemize}
\item if the capture is by copy (see below), the non-static data member
declared for the capture and the variable are treated as two different ways
of referring to the same object, which has the lifetime of the non-static
data member, and no additional copy and destruction is performed, and
\item if the capture is by reference, the variable's lifetime ends when the
closure object's lifetime ends.
\end{itemize}
\enternote
This enables an \grammarterm{init-capture} like
``\tcode{x = std::move(x)}''; the second ``\tcode{x}'' must bind to a
declaration in the surrounding context.
\exitnote
\enterexample
\begin{codeblock}
int x = 4;
auto y = [&r = x, x = x+1]()->int {
r += 2;
return x+2;
}(); // Updates \tcode{::x} to 6, and initializes \tcode{y} to 7.
\end{codeblock}
\exitexample
\pnum
A \grammarterm{lambda-expression} with an associated
\grammarterm{capture-default} that does not explicitly capture \tcode{this} or
a variable with automatic storage duration (this excludes any \grammarterm{id-expression}
that has been found to refer to an \grammarterm{init-capture}{'s} associated
\indextext{implicit capture!definition of}%
non-static data member), is said to \term{implicitly capture} the entity (i.e.,
\tcode{this} or a variable) if the \grammarterm{compound-statement}:
\begin{itemize}
\item odr-uses~(\ref{basic.def.odr}) the entity, or
\item names the entity in a potentially-evaluated
expression~(\ref{basic.def.odr}) where the enclosing full-expression depends on
a generic lambda parameter declared within the reaching scope of the
\grammarterm{lambda-expression}.
\end{itemize}
\enterexample
\begin{codeblock}
void f(int, const int (&)[2] = {}) { } // \#1
void f(const int&, const int (&)[1]) { } // \#2
void test() {
const int x = 17;
auto g = [](auto a) {
f(x); // OK: calls \#1, does not capture \tcode{x}
};
auto g2 = [=](auto a) {
int selector[sizeof(a) == 1 ? 1 : 2]{};
f(x, selector); // OK: is a dependent expression, so captures \tcode{x}
};
}
\end{codeblock}
\exitexample
All such implicitly captured
entities shall be declared within the reaching scope of the lambda expression.
\enternote The implicit capture of an entity by a nested
\grammarterm{lambda-expression} can cause its implicit capture by the containing
\grammarterm{lambda-expression} (see below). Implicit odr-uses of \tcode{this} can result
in implicit capture. \exitnote
\pnum
An entity is \defn{captured} if it is captured explicitly or implicitly. An entity
captured by a \grammarterm{lambda-expression} is odr-used~(\ref{basic.def.odr}) in the scope
containing the \grammarterm{lambda-expression}. If \tcode{this} is captured by a local
lambda expression, its nearest enclosing function shall be a non-static member function.
If a \grammarterm{lambda-expression} or an instantiation of the function call
operator template of a generic lambda odr-uses~(\ref{basic.def.odr}) \tcode{this} or a
variable with automatic storage duration from its reaching scope, that
entity shall be captured by the \grammarterm{lambda-expression}. If a
\grammarterm{lambda-expression} captures an entity and that entity is not defined or
captured in the immediately enclosing lambda expression or function, the program is
ill-formed. \enterexample
\begin{codeblock}
void f1(int i) {
int const N = 20;
auto m1 = [=]{
int const M = 30;
auto m2 = [i]{
int x[N][M]; // OK: \tcode{N} and \tcode{M} are not odr-used
x[0][0] = i; // OK: \tcode{i} is explicitly captured by \tcode{m2}
// and implicitly captured by \tcode{m1}
};
};
struct s1 {
int f;
void work(int n) {
int m = n*n;
int j = 40;
auto m3 = [this,m] {
auto m4 = [&,j] { // error: \tcode{j} not captured by \tcode{m3}
int x = n; // error: \tcode{n} implicitly captured by \tcode{m4}
// but not captured by \tcode{m3}
x += m; // OK: \tcode{m} implicitly captured by \tcode{m4}
// and explicitly captured by \tcode{m3}
x += i; // error: \tcode{i} is outside of the reaching scope
x += f; // OK: \tcode{this} captured implicitly by \tcode{m4}
// and explicitly by \tcode{m3}
};
};
}
};
}
\end{codeblock}