From Jason Turner

[stmt.iter]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpcb5rn27d/{from.md → to.md} +39 -55
tmp/tmpcb5rn27d/{from.md → to.md} RENAMED
@@ -1,7 +1,9 @@
1
  ## Iteration statements <a id="stmt.iter">[[stmt.iter]]</a>
2
 
 
 
3
  Iteration statements specify looping.
4
 
5
  ``` bnf
6
  iteration-statement:
7
  while '(' condition ')' statement
@@ -49,37 +51,17 @@ while (--x >= 0) {
49
 
50
  Thus after the `while` statement, `i` is no longer in scope.
51
 
52
  — *end example*]
53
 
54
- If a name introduced in an *init-statement* or *for-range-declaration*
55
- is redeclared in the outermost block of the substatement, the program is
56
- ill-formed.
57
-
58
- [*Example 2*:
59
-
60
- ``` cpp
61
- void f() {
62
- for (int i = 0; i < 10; ++i)
63
- int i = 0; // error: redeclaration
64
- for (int i : { 1, 2, 3 })
65
- int i = 1; // error: redeclaration
66
- }
67
- ```
68
-
69
- — *end example*]
70
-
71
  ### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
72
 
73
  In the `while` statement the substatement is executed repeatedly until
74
- the value of the condition [[stmt.select]] becomes `false`. The test
75
- takes place before each execution of the substatement.
76
 
77
- When the condition of a `while` statement is a declaration, the scope of
78
- the variable that is declared extends from its point of declaration
79
- [[basic.scope.pdecl]] to the end of the `while` *statement*. A `while`
80
- statement is equivalent to
81
 
82
  ``` bnf
83
  label ':'
84
  '{'
85
  if '(' condition ')' '{'
@@ -145,42 +127,25 @@ is equivalent to
145
  expression ';'
146
  '}'
147
  '}'
148
  ```
149
 
150
- except that names declared in the *init-statement* are in the same
151
- declarative region as those declared in the *condition*, and except that
152
- a `continue` in *statement* (not enclosed in another iteration
153
- statement) will execute *expression* before re-evaluating *condition*.
154
 
155
  [*Note 1*: Thus the first statement specifies initialization for the
156
- loop; the condition [[stmt.select]] specifies a test, sequenced before
157
- each iteration, such that the loop is exited when the condition becomes
158
  `false`; the expression often specifies incrementing that is sequenced
159
  after each iteration. — *end note*]
160
 
161
  Either or both of the *condition* and the *expression* can be omitted. A
162
  missing *condition* makes the implied `while` clause equivalent to
163
  `while(true)`.
164
 
165
- If the *init-statement* is a declaration, the scope of the name(s)
166
- declared extends to the end of the `for` statement.
167
-
168
- [*Example 1*:
169
-
170
- ``` cpp
171
- int i = 42;
172
- int a[10];
173
-
174
- for (int i = 0; i < 10; i++)
175
- a[i] = i;
176
-
177
- int j = i; // j = 42
178
- ```
179
-
180
- — *end example*]
181
-
182
  ### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
183
 
184
  The range-based `for` statement
185
 
186
  ``` bnf
@@ -207,25 +172,24 @@ where
207
  - if the *for-range-initializer* is an *expression*, it is regarded as
208
  if it were surrounded by parentheses (so that a comma operator cannot
209
  be reinterpreted as delimiting two *init-declarator*s);
210
  - *`range`*, *`begin`*, and *`end`* are variables defined for exposition
211
  only; and
212
- - *begin-expr* and *end-expr* are determined as follows:
213
  - if the *for-range-initializer* is an expression of array type `R`,
214
- *begin-expr* and *end-expr* are *`range`* and *`range`* `+` `N`,
215
  respectively, where `N` is the array bound. If `R` is an array of
216
  unknown bound or an array of incomplete type, the program is
217
  ill-formed;
218
  - if the *for-range-initializer* is an expression of class type `C`,
219
- the *unqualified-id*s `begin` and `end` are looked up in the scope
220
- of `C` as if by class member access lookup
221
- [[basic.lookup.classref]], and if both find at least one
222
- declaration, *begin-expr* and *end-expr* are `range.begin()` and
223
  `range.end()`, respectively;
224
- - otherwise, *begin-expr* and *end-expr* are `begin(range)` and
225
- `end(range)`, respectively, where `begin` and `end` are looked up in
226
- the associated namespaces [[basic.lookup.argdep]].
227
  \[*Note 1*: Ordinary unqualified lookup [[basic.lookup.unqual]] is
228
  not performed. — *end note*]
229
 
230
  [*Example 1*:
231
 
@@ -235,9 +199,29 @@ for (int& x : array)
235
  x *= 2;
236
  ```
237
 
238
  — *end example*]
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  In the *decl-specifier-seq* of a *for-range-declaration*, each
241
  *decl-specifier* shall be either a *type-specifier* or `constexpr`. The
242
  *decl-specifier-seq* shall not define a class or enumeration.
243
 
 
1
  ## Iteration statements <a id="stmt.iter">[[stmt.iter]]</a>
2
 
3
+ ### General <a id="stmt.iter.general">[[stmt.iter.general]]</a>
4
+
5
  Iteration statements specify looping.
6
 
7
  ``` bnf
8
  iteration-statement:
9
  while '(' condition ')' statement
 
51
 
52
  Thus after the `while` statement, `i` is no longer in scope.
53
 
54
  — *end example*]
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  ### The `while` statement <a id="stmt.while">[[stmt.while]]</a>
57
 
58
  In the `while` statement the substatement is executed repeatedly until
59
+ the value of the condition [[stmt.pre]] becomes `false`. The test takes
60
+ place before each execution of the substatement.
61
 
62
+ A `while` statement is equivalent to
 
 
 
63
 
64
  ``` bnf
65
  label ':'
66
  '{'
67
  if '(' condition ')' '{'
 
127
  expression ';'
128
  '}'
129
  '}'
130
  ```
131
 
132
+ except that the *init-statement* is in the same scope as the
133
+ *condition*, and except that a `continue` in *statement* (not enclosed
134
+ in another iteration statement) will execute *expression* before
135
+ re-evaluating *condition*.
136
 
137
  [*Note 1*: Thus the first statement specifies initialization for the
138
+ loop; the condition [[stmt.pre]] specifies a test, sequenced before each
139
+ iteration, such that the loop is exited when the condition becomes
140
  `false`; the expression often specifies incrementing that is sequenced
141
  after each iteration. — *end note*]
142
 
143
  Either or both of the *condition* and the *expression* can be omitted. A
144
  missing *condition* makes the implied `while` clause equivalent to
145
  `while(true)`.
146
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  ### The range-based `for` statement <a id="stmt.ranged">[[stmt.ranged]]</a>
148
 
149
  The range-based `for` statement
150
 
151
  ``` bnf
 
172
  - if the *for-range-initializer* is an *expression*, it is regarded as
173
  if it were surrounded by parentheses (so that a comma operator cannot
174
  be reinterpreted as delimiting two *init-declarator*s);
175
  - *`range`*, *`begin`*, and *`end`* are variables defined for exposition
176
  only; and
177
+ - *`begin-expr`* and *`end-expr`* are determined as follows:
178
  - if the *for-range-initializer* is an expression of array type `R`,
179
+ *`begin-expr`* and *`end-expr`* are *`range`* and *`range`* `+` `N`,
180
  respectively, where `N` is the array bound. If `R` is an array of
181
  unknown bound or an array of incomplete type, the program is
182
  ill-formed;
183
  - if the *for-range-initializer* is an expression of class type `C`,
184
+ and searches in the scope of `C` [[class.member.lookup]] for the
185
+ names `begin` and `end` each find at least one declaration,
186
+ *`begin-expr`* and *`end-expr`* are `range.begin()` and
 
187
  `range.end()`, respectively;
188
+ - otherwise, *`begin-expr`* and *`end-expr`* are `begin(range)` and
189
+ `end(range)`, respectively, where `begin` and `end` undergo
190
+ argument-dependent lookup [[basic.lookup.argdep]].
191
  \[*Note 1*: Ordinary unqualified lookup [[basic.lookup.unqual]] is
192
  not performed. — *end note*]
193
 
194
  [*Example 1*:
195
 
 
199
  x *= 2;
200
  ```
201
 
202
  — *end example*]
203
 
204
+ [*Note 2*: The lifetime of some temporaries in the
205
+ *for-range-initializer* is extended to cover the entire loop
206
+ [[class.temporary]]. — *end note*]
207
+
208
+ [*Example 2*:
209
+
210
+ ``` cpp
211
+ using T = std::list<int>;
212
+ const T& f1(const T& t) { return t; }
213
+ const T& f2(T t) { return t; }
214
+ T g();
215
+
216
+ void foo() {
217
+ for (auto e : f1(g())) {} // OK, lifetime of return value of g() extended
218
+ for (auto e : f2(g())) {} // undefined behavior
219
+ }
220
+ ```
221
+
222
+ — *end example*]
223
+
224
  In the *decl-specifier-seq* of a *for-range-declaration*, each
225
  *decl-specifier* shall be either a *type-specifier* or `constexpr`. The
226
  *decl-specifier-seq* shall not define a class or enumeration.
227