-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstatements.tex
130 lines (113 loc) · 4.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
%!TEX root = std.tex
\rSec0[stmt]{Statements}
\rSec1[stmt.iter]{Iteration statements}
\rSec2[stmt.ranged]{The range-based \tcode{for} statement}
\pnum
\enternote This clause is presented as a set of differences to apply to
\cxxref{stmt.ranged} to allow differently typed begin and end
iterators, as in C++17. \exitnote
\begin{quote}
\pnum
\begin{removedblock}
For a range-based \tcode{for} statement of the form
\begin{bnf}
\terminal{for} \terminal{(} for-range-declaration \terminal{:} expression \terminal{)} statement
\end{bnf}
%
let \grammarterm{range-init} be equivalent to the \grammarterm{expression} surrounded by parentheses
\begin{bnf}
\terminal{(} expression \terminal{)}
\end{bnf}
%
and for a range-based \tcode{for} statement of the form
\begin{bnf}
\terminal{for} \terminal{(} for-range-declaration \terminal{:} braced-init-list \terminal{)} statement
\end{bnf}
%
let \grammarterm{range-init} be equivalent to the \grammarterm{braced-init-list}.
%
In each case, a range-based \tcode{for} statement is equivalent to
\begin{codeblock}
{
auto && __range = range-init;
for ( auto __begin = begin-expr,
__end = end-expr;
__begin != __end;
++__begin ) {
@\placeholder{for-range-declaration}@= *__begin;
@\placeholder{statement}@
}
}
\end{codeblock}
\end{removedblock}
\begin{addedblock}
The range-based \tcode{for} statement
\begin{bnf}
\terminal{for} \terminal{(} for-range-declaration \terminal{:} for-range-initializer \terminal{)} statement
\end{bnf}
%
is equivalent to
\begin{codeblock}
{
auto &&__range = @\grammarterm{for-range-initializer}@;
auto __begin = @\grammarterm{begin-expr}@;
auto __end = @\grammarterm{end-expr}@;
for ( ; __begin != __end; ++__begin ) {
@\grammarterm{for-range-declaration}@ = *__begin;
@\grammarterm{statement}@
}
}
\end{codeblock}
\end{addedblock}
%
where
\begin{itemize}
\item
\added{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 \tcode{\removed{_RangeT}}\removed{ is the type of the
\grammarterm{}{expression}, and \textit{begin-expr} and \textit{end-expr} are
determined as follows:}
\item
\added{\grammarterm{begin-expr} and \grammarterm{end-expr} are determined as follows:}
\begin{itemize}
\item
if \tcode{\removed{_RangeT}}\added{the \grammarterm{for-range-initializer}} is an
\added{expression of} array type \added{\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{\removed{_RangeT}}\tcode{\added{R}} is an array of unknown
\changed{size}{bound} or an array of incomplete type, the program is ill-formed;
\item
if \tcode{\removed{_RangeT}}\added{the \grammarterm{for-range-initializer}} is
\changed{a}{an expression of} class type \added{\tcode{C}}, the
\grammarterm{unqualified-id}{s} \tcode{begin} and \tcode{end} are looked up in
the scope of \changed{class \tcode{\mbox{_RangeT}}}{\tcode{C}} as if by class
member access lookup~(\stdcxxref{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~(\stdcxxref{basic.lookup.argdep}).
\enternote Ordinary unqualified lookup~(\stdcxxref{basic.lookup.unqual}) is not
performed. \exitnote
\end{itemize}
\end{itemize}
\enterexample
\begin{codeblock}
int array[5] = { 1, 2, 3, 4, 5 };
for (int& x : array)
x *= 2;
\end{codeblock}
\exitexample%
\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.
\end{quote}