-
Notifications
You must be signed in to change notification settings - Fork 738
/
statements.tex
1051 lines (897 loc) · 34.2 KB
/
statements.tex
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[stmt.stmt]{Statements}%
\indextext{statement|(}
%gram: \rSec1[gram.stmt]{Statements}
%gram:
\indextext{block statement|see{statement, compound}}
\pnum
Except as indicated, statements are executed in sequence.
\begin{bnf}
\nontermdef{statement}\br
labeled-statement\br
attribute-specifier-seq\opt expression-statement\br
attribute-specifier-seq\opt compound-statement\br
attribute-specifier-seq\opt selection-statement\br
attribute-specifier-seq\opt iteration-statement\br
attribute-specifier-seq\opt jump-statement\br
declaration-statement\br
attribute-specifier-seq\opt try-block
\nontermdef{init-statement}\br
expression-statement\br
simple-declaration
\nontermdef{condition}\br
expression\br
attribute-specifier-seq\opt decl-specifier-seq declarator \terminal{=} initializer-clause\br
attribute-specifier-seq\opt decl-specifier-seq declarator braced-init-list
\end{bnf}
The optional \grammarterm{attribute-specifier-seq} appertains to the respective statement.
\pnum
\indextext{\idxgram{condition}{s}!rules~for}%
The rules for \grammarterm{}{condition}{s} apply both to
\grammarterm{selection-statement}{s} and to the \tcode{for} and \tcode{while}
statements~(\ref{stmt.iter}). The \grammarterm{}{declarator} shall not
specify a function or an array. The \grammarterm{decl-specifier-seq} shall not
define a class or enumeration. If the \tcode{auto} \nonterminal{type-specifier} appears in
the \nonterminal{decl-specifier-seq},
the type of the identifier being declared is deduced from the initializer as described in~\ref{dcl.spec.auto}.
\pnum
\indextext{statement!declaration in \tcode{if}}%
\indextext{statement!declaration in \tcode{switch}}%
A name introduced by a declaration in a \grammarterm{}{condition} (either
introduced by the \grammarterm{decl-specifier-seq} or the
\grammarterm{}{declarator} of the condition) is in scope from its point of
declaration until the end of the substatements controlled by the
condition. If the name is re-declared in the outermost block of a
substatement controlled by the condition, the declaration that
re-declares the name is ill-formed.
\begin{example}
\begin{codeblock}
if (int x = f()) {
int x; // ill-formed, redeclaration of \tcode{x}
}
else {
int x; // ill-formed, redeclaration of \tcode{x}
}
\end{codeblock}
\end{example}
\pnum
The value of a \grammarterm{}{condition} that is an initialized declaration
in a statement other than a \tcode{switch} statement is the value of the
declared variable
contextually converted to \tcode{bool} (Clause~\ref{conv}).
If that
conversion is ill-formed, the program is ill-formed. The value of a
\grammarterm{}{condition} that is an initialized declaration in a
\tcode{switch} statement is the value of the declared variable if it has
integral or enumeration type, or of that variable implicitly converted
to integral or enumeration type otherwise. The value of a
\grammarterm{}{condition} that is an expression is the value of the
expression, contextually converted to \tcode{bool}
for statements other
than \tcode{switch};
if that conversion is ill-formed, the program is
ill-formed. The value of the condition will be referred to as simply
``the condition'' where the usage is unambiguous.
\pnum
If a \grammarterm{}{condition} can be syntactically resolved as either an
expression or the declaration of a block-scope name, it is interpreted as a
declaration.
\pnum
In the \grammarterm{decl-specifier-seq} of a \grammarterm{condition}, each
\grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
or \tcode{constexpr}.
\rSec1[stmt.label]{Labeled statement}%
\indextext{statement!labeled}
\pnum
\indextext{statement!labeled}%
\indextext{\idxcode{:}!label~specifier}%
A statement can be labeled.
\begin{bnf}
\nontermdef{labeled-statement}\br
attribute-specifier-seq\opt identifier \terminal{:} statement\br
attribute-specifier-seq\opt{} \terminal{case} constant-expression \terminal{:} statement\br
attribute-specifier-seq\opt{} \terminal{default :} statement
\end{bnf}
The optional \grammarterm{attribute-specifier-seq} appertains to the label. An
\defn{identifier label} declares the identifier. The only use of an
identifier label is as the target of a
\indextext{statement!\idxcode{goto}}%
\tcode{goto}.
\indextext{label!scope~of}%
The scope of a label is the function in which it appears. Labels shall
not be redeclared within a function. A label can be used in a
\tcode{goto} statement before its declaration.
\indextext{name~space!label}%
Labels have their own name space and do not interfere with other
identifiers.
\begin{note}
A label may have the same name as another declaration in the same scope or a
\grammarterm{template-parameter} from an enclosing scope. Unqualified name
lookup~(\ref{basic.lookup.unqual}) ignores labels.
\end{note}
\pnum
\indextext{label!\idxcode{case}}%
\indextext{label!\idxcode{default}}%
Case labels and default labels shall occur only in switch statements.
\rSec1[stmt.expr]{Expression statement}%
\indextext{statement!expression}
\pnum
Expression statements have the form
\begin{bnf}
\nontermdef{expression-statement}\br
expression\opt{} \terminal{;}
\end{bnf}
The expression is
a discarded-value expression (Clause~\ref{expr}).
All
\indextext{side effects}%
side effects from an expression statement
are completed before the next statement is executed.
\indextext{statement!null}%
\indextext{statement!empty}%
An expression statement with the expression missing is called
a \defn{null statement}.
\begin{note}
Most statements are expression statements --- usually assignments or
function calls. A null statement is useful to carry a label just before
the \tcode{\}} of a compound statement and to supply a null body to an
iteration statement such as a \tcode{while}
statement~(\ref{stmt.while}).
\end{note}
\rSec1[stmt.block]{Compound statement or block}%
\indextext{statement!compound}%
\indextext{\idxcode{\{\}}!block statement}%
\pnum
So that several statements can be used where one is expected, the
compound statement (also, and equivalently, called ``block'') is
provided.
\begin{bnf}
\nontermdef{compound-statement}\br
\terminal{\{} statement-seq\opt{} \terminal{\}}
\end{bnf}
\begin{bnf}
\nontermdef{statement-seq}\br
statement\br
statement-seq statement
\end{bnf}
A compound statement defines a block scope~(\ref{basic.scope}).
\begin{note}
A declaration is a \grammarterm{}{statement}~(\ref{stmt.dcl}).
\end{note}
\rSec1[stmt.select]{Selection statements}%
\indextext{statement!selection|(}
\pnum
Selection statements choose one of several flows of control.
\indextext{statement!\idxcode{if}}%
\indextext{statement!\idxcode{switch}}%
%
\begin{bnf}
\nontermdef{selection-statement}\br
\terminal{if constexpr\opt (} init-statement\opt condition \terminal{)} statement\br
\terminal{if constexpr\opt (} init-statement\opt condition \terminal{)} statement \terminal{else} statement\br
\terminal{switch (} init-statement\opt condition \terminal{)} statement
\end{bnf}
See~\ref{dcl.meaning} for the optional \grammarterm{attribute-specifier-seq} in a condition.
\begin{note}
An \grammarterm{init-statement} ends with a semicolon.
\end{note}
In Clause~\ref{stmt.stmt}, the term \term{substatement} refers to
the contained \grammarterm{}{statement} or \grammarterm{}{statement}{s} that appear
in the syntax notation.
\indextext{scope!\idxgram{selection-statement}}%
The substatement in a \grammarterm{selection-statement} (each substatement,
in the \tcode{else} form of the \tcode{if} statement) implicitly defines
a block scope~(\ref{basic.scope}). If the substatement in a
selection-statement is a single statement and not a
\grammarterm{compound-statement,} it is as if it was rewritten to be a
compound-statement containing the original substatement.
\begin{example}
\begin{codeblock}
if (x)
int i;
\end{codeblock}
can be equivalently rewritten as
\begin{codeblock}
if (x) {
int i;
}
\end{codeblock}
Thus after the \tcode{if} statement, \tcode{i} is no longer in scope.
\end{example}
\rSec2[stmt.if]{The \tcode{if} statement}%
\indextext{statement!\idxcode{if}}
\pnum
If the condition~(\ref{stmt.select}) yields \tcode{true} the first
substatement is executed. If the \tcode{else} part of the selection
statement is present and the condition yields \tcode{false}, the second
substatement is executed. If the first substatement is reached via a
label, the condition is not evaluated and the second substatement is
not executed. In the second form of \tcode{if} statement
(the one including \tcode{else}), if the first substatement is also an
\tcode{if} statement then that inner \tcode{if} statement shall contain
an \tcode{else} part.\footnote{In other words, the \tcode{else} is associated with the nearest un-elsed
\tcode{if}.}
\pnum
If the \tcode{if} statement is of the form \tcode{if constexpr}, the value
of the condition shall be a contextually
converted constant expression of type \tcode{bool}~(\ref{expr.const}); this
form is called a \defn{constexpr if} statement. If the value of the
converted condition is \tcode{false}, the first substatement is a
\defn{discarded statement}, otherwise the second substatement, if
present, is a discarded statement. During the instantation of an
enclosing templated entity (Clause~\ref{temp}), if the condition is
not value-dependent after its instantiation, the discarded substatement
(if any) is not instantiated.
\begin{note}
Odr-uses~(\ref{basic.def.odr}) in a discarded statement do not require
an entity to be defined.
\end{note}
A \tcode{case} or \tcode{default} label appearing within such an
\tcode{if} statement shall be associated with a \tcode{switch}
statement~(\ref{stmt.switch}) within the same \tcode{if} statement.
A label~(\ref{stmt.label}) declared in a substatement of a \tcode{constexpr if}
statement shall only be referred to by a statement~(\ref{stmt.goto}) in
the same substatement.
\begin{example}
\begin{codeblock}
template<typename T, typename ... Rest> void g(T&& p, Rest&& ...rs) {
// ... handle \tcode{p}
if constexpr (sizeof...(rs) > 0)
g(rs...); // never instantiated with an empty argument list.
}
extern int x; // no definition of \tcode{x} required
int f() {
if constexpr (true)
return 0;
else if (x)
return x;
else
return -x;
}
}
\end{codeblock}
\end{example}
\pnum
An \tcode{if} statement of the form
\begin{ncbnf}
\terminal{if constexpr\opt (} init-statement condition \terminal{)} statement
\end{ncbnf}
is equivalent to
\begin{ncbnftab}
\terminal{\{}\br
\>init-statement\br
\>\terminal{if constexpr\opt (} condition \terminal{)} statement\br
\terminal{\}}
\end{ncbnftab}
and an \tcode{if} statement of the form
\begin{ncbnf}
\terminal{if constexpr\opt (} init-statement condition \terminal{)} statement \terminal{else} statement
\end{ncbnf}
is equivalent to
\begin{ncbnftab}
\terminal{\{}\br
\>init-statement\br
\>\terminal{if constexpr\opt (} condition \terminal{)} statement \terminal{else} statement\br
\terminal{\}}
\end{ncbnftab}
except that names declared in the \grammarterm{init-statement} are in
the same declarative region as those declared in the
\grammarterm{condition}.
\rSec2[stmt.switch]{The \tcode{switch} statement}%
\indextext{statement!\idxcode{switch}}
\pnum
The \tcode{switch} statement causes control to be transferred to one of
several statements depending on the value of a condition.
\pnum
The condition shall be of integral type, enumeration type, or class
type. If of class type, the
condition is contextually implicitly converted (Clause~\ref{conv}) to
an integral or enumeration type.
If the (possibly converted) type is subject to integral
promotions~(\ref{conv.prom}), the condition is converted
to the promoted type.
Any
statement within the \tcode{switch} statement can be labeled with one or
more case labels as follows:
\begin{ncbnf}
\indextext{label!\idxcode{case}}%
\terminal{case} constant-expression \terminal{:}
\end{ncbnf}
where the \grammarterm{constant-expression} shall be
a converted constant expression~(\ref{expr.const}) of the
adjusted type of the switch condition. No two of the case constants in
the same switch shall have the same value after conversion.
\pnum
\indextext{label!\idxcode{default}}%
There shall be at most one label of the form
\begin{codeblock}
default :
\end{codeblock}
within a \tcode{switch} statement.
\pnum
Switch statements can be nested; a \tcode{case} or \tcode{default} label
is associated with the smallest switch enclosing it.
\pnum
When the \tcode{switch} statement is executed, its condition is
evaluated and compared with each case constant.
\indextext{label!\idxcode{case}}%
If one of the case constants is equal to the value of the condition,
control is passed to the statement following the matched case label. If
no case constant matches the condition, and if there is a
\indextext{label!\idxcode{default}}%
\tcode{default} label, control passes to the statement labeled by the
default label. If no case matches and if there is no \tcode{default}
then none of the statements in the switch is executed.
\pnum
\tcode{case} and \tcode{default} labels in themselves do not alter the
flow of control, which continues unimpeded across such labels. To exit
from a switch, see \tcode{break},~\ref{stmt.break}.
\begin{note}
Usually, the substatement that is the subject of a switch is compound
and \tcode{case} and \tcode{default} labels appear on the top-level
statements contained within the (compound) substatement, but this is not
required.
\indextext{statement!declaration~in \tcode{switch}}%
Declarations can appear in the substatement of a
\grammarterm{switch-statement}.
\end{note}%
\indextext{statement!selection|)}
\pnum
A \tcode{switch} statement of the form
\begin{ncbnf}
\terminal{switch (} init-statement condition \terminal{)} statement
\end{ncbnf}
is equivalent to
\begin{ncbnftab}
\terminal{\{}\br
\>init-statement\br
\>\terminal{switch (} condition \terminal{)} statement\br
\terminal{\}}
\end{ncbnftab}
except that names declared in the \grammarterm{init-statement} are in
the same declarative region as those declared in the
\grammarterm{condition}.
\rSec1[stmt.iter]{Iteration statements}%
\indextext{statement!iteration|(}
\pnum
Iteration statements specify looping.
\indextext{statement!\idxcode{while}}%
\indextext{statement!\idxcode{do}}%
\indextext{statement!\idxcode{for}}%
%
\begin{bnf}
\nontermdef{iteration-statement}\br
\terminal{while (} condition \terminal{)} statement\br
\terminal{do} statement \terminal{while (} expression \terminal{) ;}\br
\terminal{for (} init-statement condition\opt{} \terminal{;} expression\opt{} \terminal{)} statement\br
\terminal{for (} for-range-declaration \terminal{:} for-range-initializer \terminal{)} statement
\end{bnf}
\begin{bnf}
\nontermdef{for-range-declaration}\br
attribute-specifier-seq\opt decl-specifier-seq declarator\br
attribute-specifier-seq\opt decl-specifier-seq ref-qualifier\opt{} \terminal{[} identifier-list \terminal{]}
\end{bnf}
\begin{bnf}
\nontermdef{for-range-initializer}\br
expr-or-braced-init-list\br
\end{bnf}
See~\ref{dcl.meaning} for the optional \grammarterm{attribute-specifier-seq} in a
\grammarterm{for-range-declaration}.
\begin{note}
An \grammarterm{init-statement} ends with a semicolon.
\end{note}
\pnum
The substatement in an \grammarterm{iteration-statement} implicitly defines
a block scope~(\ref{basic.scope}) which is entered and exited each time
through the loop.
\indextext{scope!\idxgram{iteration-statement}}%
If the substatement in an iteration-statement is a single statement and
not a \grammarterm{compound-statement,} it is as if it was rewritten to be
a compound-statement containing the original statement.
\begin{example}
\begin{codeblock}
while (--x >= 0)
int i;
\end{codeblock}
can be equivalently rewritten as
\begin{codeblock}
while (--x >= 0) {
int i;
}
\end{codeblock}
\pnum
Thus after the \tcode{while} statement, \tcode{i} is no longer in scope.
\end{example}
\pnum
\begin{note}
The requirements on \grammarterm{}{condition}{s} in iteration statements are
described in~\ref{stmt.select}.
\end{note}
\rSec2[stmt.while]{The \tcode{while} statement}%
\indextext{statement!\idxcode{while}}
\pnum
In the \tcode{while} statement the substatement is executed repeatedly
until the value of the condition~(\ref{stmt.select}) becomes
\tcode{false}. The test takes place before each execution of the
substatement.
\pnum
\indextext{statement!declaration~in \tcode{while}}%
When the condition of a \tcode{while} statement is a declaration, the scope of
the variable that is declared extends from its point of
declaration~(\ref{basic.scope.pdecl}) to the end of the \tcode{while}
\grammarterm{}{statement}. A \tcode{while} statement of the form
\begin{codeblock}
while (T t = x) @\grammarterm{statement}@
\end{codeblock}
is equivalent to
\begin{codeblock}
label:
{ // start of condition scope
T t = x;
if (t) {
@\grammarterm{statement}@
goto label;
}
} // end of condition scope
\end{codeblock}
The variable created in a condition is destroyed and created with each
iteration of the loop.
\begin{example}
\begin{codeblock}
struct A {
int val;
A(int i) : val(i) { }
~A() { }
operator bool() { return val != 0; }
};
int i = 1;
while (A a = i) {
// ...
i = 0;
}
\end{codeblock}
In the while-loop, the constructor and destructor are each called twice,
once for the condition that succeeds and once for the condition that
fails.
\end{example}
\rSec2[stmt.do]{The \tcode{do} statement}%
\indextext{statement!\idxcode{do}}
\pnum
The expression is contextually converted to \tcode{bool} (Clause~\ref{conv});
if that conversion is ill-formed, the program is ill-formed.
\pnum
In the \tcode{do} statement the substatement is executed repeatedly
until the value of the expression becomes \tcode{false}. The test takes
place after each execution of the statement.
\rSec2[stmt.for]{The \tcode{for} statement}%
\indextext{statement!\idxcode{for}}
\pnum
The \tcode{for} statement
\begin{ncbnf}
\terminal{for (} init-statement condition\opt{} \terminal{;} expression\opt{} \terminal{)} statement
\end{ncbnf}
is equivalent to
\begin{ncbnftab}
\terminal{\{}\br
\>init-statement\br
\>\terminal{while (} condition \terminal{) \{}\br
\>\>statement\br
\>\>expression \terminal{;}\br
\>\terminal{\}}\br
\terminal{\}}
\end{ncbnftab}
except that names declared in the \grammarterm{init-statement} are in
the same declarative region as those declared in the
\grammarterm{condition}, and except that a
\indextext{statement!\tcode{continue}~in \tcode{for}}%
\tcode{continue} in \grammarterm{statement} (not enclosed in another
iteration statement) will execute \grammarterm{expression} before
re-evaluating \grammarterm{condition}.
\begin{note}
Thus the first statement specifies initialization for the loop; the
condition~(\ref{stmt.select}) specifies a test, sequenced before each
iteration, such that the loop is exited when the condition becomes
\tcode{false}; the expression often specifies incrementing that is
sequenced after each iteration.
\end{note}
\pnum
Either or both of the \grammarterm{condition}
and the \grammarterm{expression} can be omitted.
A missing \grammarterm{condition}
makes the implied \tcode{while} clause
equivalent to \tcode{while(true)}.
\pnum
\indextext{statement!declaration~in \tcode{for}}%
\indextext{\idxcode{for}!scope~of declaration~in}%
If the \grammarterm{init-statement} is a declaration, the scope of the
name(s) declared extends to the end of the \tcode{for} statement.
\begin{example}
\begin{codeblock}
int i = 42;
int a[10];
for (int i = 0; i < 10; i++)
a[i] = i;
int j = i; // \tcode{j = 42}
\end{codeblock}
\end{example}
\rSec2[stmt.ranged]{The range-based \tcode{for} statement}%
\indextext{statement!range~based \idxcode{for}}
\pnum
The range-based \tcode{for} statement
\begin{ncbnf}
\terminal{for (} for-range-declaration \terminal{:} for-range-initializer \terminal{)} statement
\end{ncbnf}
is equivalent to
\begin{ncbnftab}
\terminal{\{}\br
\>\terminal{auto \&\&__range =} for-range-initializer \terminal{;}\br
\>\terminal{auto __begin =} begin-expr \terminal{;}\br
\>\terminal{auto __end =} end-expr \terminal{;}\br
\>\terminal{for ( ; __begin != __end; ++__begin ) \{}\br
\>\>for-range-declaration \terminal{= *__begin;}\br
\>\>statement\br
\>\terminal{\}}\br
\terminal{\}}
\end{ncbnftab}
where
\begin{itemize}
\item
if the \grammarterm{for-range-initializer} is an \grammarterm{expression},
it is regarded as if it were surrounded by parentheses (so that a comma operator
cannot be reinterpreted as delimiting two \grammarterm{init-declarators});
\item \tcode{__range}, \tcode{__begin}, and \tcode{__end} are variables defined for
exposition only; and
\item
\textit{begin-expr} and \textit{end-expr} are determined as follows:
\begin{itemize}
\item if the \grammarterm{for-range-initializer} is an expression of
array type \tcode{R}, \textit{begin-expr} and \textit{end-expr} are
\tcode{__range} and \tcode{__range + __bound}, respectively, where \tcode{__bound} is
the array bound. If \tcode{R} is an array of unknown size or an array of
incomplete type, the program is ill-formed;
\item if the \grammarterm{for-range-initializer} is an expression of
class type \tcode{C}, the \grammarterm{unqualified-id}{s}
\tcode{begin} and \tcode{end} are looked up in the scope of \tcode{C}
as if by class member access lookup~(\ref{basic.lookup.classref}), and if either
(or both) finds at least one declaration, \grammarterm{begin-expr} and
\grammarterm{end-expr} are \tcode{__range.begin()} and \tcode{__range.end()},
respectively;
\item otherwise, \textit{begin-expr} and \textit{end-expr} are \tcode{begin(__range)}
and \tcode{end(__range)}, respectively, where \tcode{begin} and \tcode{end} are looked
up in the associated namespaces~(\ref{basic.lookup.argdep}).
\begin{note} Ordinary unqualified lookup~(\ref{basic.lookup.unqual}) is not
performed. \end{note}
\end{itemize}
\end{itemize}
\begin{example}
\begin{codeblock}
int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
x *= 2;
\end{codeblock}
\end{example}%
\indextext{statement!iteration|)}
\pnum
In the \grammarterm{decl-specifier-seq} of a \grammarterm{for-range-declaration},
each \grammarterm{decl-specifier} shall be either a \grammarterm{type-specifier}
or \tcode{constexpr}. The \grammarterm{decl-specifier-seq} shall not define a
class or enumeration.
\rSec1[stmt.jump]{Jump statements}%
\indextext{statement!jump}
\pnum
Jump statements unconditionally transfer control.
\indextext{statement!jump}%
\indextext{statement!\idxcode{break}}%
\indextext{statement!\idxcode{continue}}%
\indextext{return statement@\tcode{return} statement|see{\tcode{return}}}%
\indextext{\idxcode{return}}%
\indextext{statement!\idxcode{goto}}%
%
\begin{bnf}
\nontermdef{jump-statement}\br
\terminal{break ;}\br
\terminal{continue ;}\br
\terminal{return} expr-or-braced-init-list\opt{} \terminal{;}\br
\terminal{goto} identifier \terminal{;}
\end{bnf}
\pnum
\indextext{local~variable!destruction~of}%
\indextext{scope!destructor~and exit~from}%
On exit from a scope (however accomplished), objects with automatic storage
duration~(\ref{basic.stc.auto}) that have been constructed in that scope are destroyed
in the reverse order of their construction. \begin{note} For temporaries,
see~\ref{class.temporary}. \end{note} Transfer out of a loop, out of a block, or back
past
an initialized variable with automatic storage duration involves the
destruction of objects with automatic storage duration that are in
scope at the point transferred from but not at the point transferred to.
(See~\ref{stmt.dcl} for transfers into blocks).
\begin{note}
However, the program can be terminated (by calling
\indextext{\idxcode{exit}}%
\indexlibrary{\idxcode{exit}}%
\tcode{std::exit()} or
\indextext{\idxcode{abort}}%
\indexlibrary{\idxcode{abort}}%
\tcode{std::abort()}~(\ref{support.start.term}), for example) without
destroying class objects with automatic storage duration.
\end{note}
\rSec2[stmt.break]{The \tcode{break} statement}%
\indextext{statement!\idxcode{break}}
\pnum
The \tcode{break} statement shall occur only in an
\indextext{\idxgram{iteration-statement}}%
\indextext{statement!\idxcode{switch}}%
\grammarterm{iteration-statement} or a \tcode{switch} statement and causes
termination of the smallest enclosing \grammarterm{iteration-statement} or
\tcode{switch} statement; control passes to the statement following the
terminated statement, if any.
\rSec2[stmt.cont]{The \tcode{continue} statement}%
\indextext{statement!\idxcode{continue}}
\pnum
The
\tcode{continue}
statement shall occur only in an
\indextext{\idxgram{iteration-statement}}%
\grammarterm{iteration-statement}
and causes control to pass to the loop-continuation portion of the
smallest enclosing \grammarterm{iteration-statement}, that is, to the end
of the loop. More precisely, in each of the statements
\begin{minipage}{.30\hsize}
\begin{codeblock}
while (foo) {
{
// ...
}
contin: ;
}
\end{codeblock}
\end{minipage}
\begin{minipage}{.30\hsize}
\begin{codeblock}
do {
{
// ...
}
contin: ;
} while (foo);
\end{codeblock}
\end{minipage}
\begin{minipage}{.30\hsize}
\begin{codeblock}
for (;;) {
{
// ...
}
contin: ;
}
\end{codeblock}
\end{minipage}
a \tcode{continue} not contained in an enclosed iteration statement is
equivalent to \tcode{goto} \tcode{contin}.
\rSec2[stmt.return]{The \tcode{return} statement}%
\indextext{\idxcode{return}}%
\indextext{function~return|see{\tcode{return}}}%
\pnum
A function returns to its caller by the \tcode{return} statement.
\pnum
The \grammarterm{expr-or-braced-init-list}
of a return statement is called its operand. A return statement with
no operand shall be used only in a function whose return type is
\cv\ \tcode{void}, a constructor~(\ref{class.ctor}), or a
destructor~(\ref{class.dtor}).
\indextext{\idxcode{return}!constructor~and}%
\indextext{\idxcode{return}!constructor~and}%
A return statement with an operand of type \tcode{void} shall be used only
in a function whose return type is \cv\ \tcode{void}.
A return statement with any other operand shall be used only
in a function whose return type is not \cv\ \tcode{void};
\indextext{conversion!return~type}%
the return statement initializes the
glvalue result or prvalue result object of the (explicit or implicit) function call
by copy-initialization~(\ref{dcl.init}) from the operand.
\begin{note}
A return statement can involve
an invocation of a constructor to perform a copy or move of the operand
if it is not a prvalue or if its type differs from the return type of the function.
A copy operation associated with a return statement may be elided or
converted to a move operation if an automatic storage duration variable is returned~(\ref{class.copy}).
\end{note}
\begin{example}
\begin{codeblock}
std::pair<std::string,int> f(const char* p, int x) {
return {p,x};
}
\end{codeblock}
\end{example}
Flowing off the end of
a constructor,
a destructor, or
a function with a \cv\ \tcode{void} return type is
equivalent to a \tcode{return} with no operand.
Otherwise, flowing off the end of a function
other than \tcode{main}~(\ref{basic.start.main})
results in undefined behavior.
\pnum
The copy-initialization of the result of the call is sequenced before the
destruction of temporaries at the end of the full-expression established
by the operand of the return statement, which, in turn, is sequenced
before the destruction of local variables~(\ref{stmt.jump}) of the block
enclosing the return statement.
\rSec2[stmt.goto]{The \tcode{goto} statement}%
\indextext{statement!\idxcode{goto}}
\pnum
The \tcode{goto} statement unconditionally transfers control to the
statement labeled by the identifier. The identifier shall be a
\indextext{label}%
label~(\ref{stmt.label}) located in the current function.
\rSec1[stmt.dcl]{Declaration statement}%
\indextext{statement!declaration}
\pnum
A declaration statement introduces one or more new identifiers into a
block; it has the form
\begin{bnf}
\nontermdef{declaration-statement}\br
block-declaration
\end{bnf}
If an identifier introduced by a declaration was previously declared in
an outer block,
\indextext{declaration~hiding|see{name~hiding}}%
\indextext{name~hiding}%
\indextext{block~structure}%
the outer declaration is hidden for the remainder of the block, after
which it resumes its force.
\pnum
\indextext{block!initialization~in}%
\indextext{initialization!automatic}%
Variables with automatic storage duration~(\ref{basic.stc.auto}) are
initialized each time their \grammarterm{declaration-statement} is executed.
\indextext{local~variable!destruction~of}%
Variables with automatic storage duration declared in the block are
destroyed on exit from the block~(\ref{stmt.jump}).
\pnum
\indextext{initialization!jump~past}%
\indextext{\idxcode{goto}!initialization~and}%
It is possible to transfer into a block, but not in a way that bypasses
declarations with initialization. A program that jumps\footnote{The transfer from the condition of a \tcode{switch} statement to a
\tcode{case} label is considered a jump in this respect.}
from a point where a variable with automatic storage duration is
not in scope to a point where it is in scope is ill-formed unless the
variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an
\grammarterm{}{initializer}~(\ref{dcl.init}).
\begin{example}
\begin{codeblock}
void f() {
// ...
goto lx; // ill-formed: jump into scope of \tcode{a}
// ...
ly:
X a = 1;
// ...
lx:
goto ly; // OK, jump implies destructor
// call for \tcode{a} followed by construction
// again immediately following label \tcode{ly}
}
\end{codeblock}
\end{example}
\pnum
\indextext{initialization!automatic}%
\indextext{initialization!dynamic~block-scope}%
\indextext{initialization!local \tcode{static}}%
\indextext{initialization!local \tcode{thread_local}}%
Dynamic initialization of a block-scope variable with
static storage duration~(\ref{basic.stc.static}) or
thread storage duration~(\ref{basic.stc.thread}) is performed
the first time control passes through its declaration; such a variable is
considered initialized upon the completion of its initialization. If the
initialization exits by throwing an exception, the initialization is not
complete, so it will be tried again the next time control enters the
declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization.\footnote{The implementation must not introduce any deadlock around execution of the initializer.} If control re-enters the declaration recursively while
the variable is being initialized, the behavior is undefined.
\begin{example}
\begin{codeblock}
int foo(int i) {
static int s = foo(2*i); // recursive call - undefined
return i+1;
}
\end{codeblock}
\end{example}
\pnum
\indextext{\idxcode{static}!destruction~of local}%
The destructor for a block-scope object with static or thread storage duration will be
executed if and only if it was constructed.
\begin{note}
\ref{basic.start.term} describes the order in which block-scope objects with
static and thread storage duration are destroyed.
\end{note}
\rSec1[stmt.ambig]{Ambiguity resolution}%
\indextext{ambiguity!declaration~versus expression}
\pnum
There is an ambiguity in the grammar involving
\grammarterm{expression-statement}{s} and \grammarterm{declaration}{s}: An
\grammarterm{expression-statement} with a function-style explicit type
conversion~(\ref{expr.type.conv}) as its leftmost subexpression can be
indistinguishable from a \grammarterm{declaration} where the first
\grammarterm{declarator} starts with a \tcode{(}. In those cases the
\grammarterm{statement} is a \grammarterm{declaration}.
\pnum
\begin{note}
If the \grammarterm{statement} cannot syntactically be a
\grammarterm{declaration}, there is no ambiguity,
so this rule does not apply.
The whole \grammarterm{statement} might need to be examined
to determine whether this is the case. This resolves the meaning
of many examples.
\begin{example}
Assuming \tcode{T} is a
\grammarterm{simple-type-specifier}~(\ref{dcl.type}),
\begin{codeblock}
T(a)->m = 7; // expression-statement
T(a)++; // expression-statement
T(a,5)<<c; // expression-statement
T(*d)(int); // declaration
T(e)[5]; // declaration
T(f) = { 1, 2 }; // declaration
T(*g)(double(3)); // declaration
\end{codeblock}
In the last example above, \tcode{g}, which is a pointer to \tcode{T},
is initialized to \tcode{double(3)}. This is of course ill-formed for
semantic reasons, but that does not affect the syntactic analysis.
\end{example}
The remaining cases are \grammarterm{declaration}{s}.
\begin{example}
\begin{codeblock}
class T {
// ...
public:
T();
T(int);