Difference between revisions of "cpp/language/range-for"
(+co_await) |
|||
Line 8: | Line 8: | ||
{{sdsc begin}} | {{sdsc begin}} | ||
{{sdsc|{{spar|attr}}{{mark optional}} {{ttb|for (}} {{spar|range_declaration}} {{ttb|:}} {{spar|range_expression}} {{ttb|)}} {{spar|loop_statement}} }} | {{sdsc|{{spar|attr}}{{mark optional}} {{ttb|for (}} {{spar|range_declaration}} {{ttb|:}} {{spar|range_expression}} {{ttb|)}} {{spar|loop_statement}} }} | ||
− | {{sdsc|notes={{mark since c++20}}|{{spar|attr}}{{mark optional}} {{ttb|for | + | {{sdsc|notes={{mark since c++20}}|{{spar|attr}}{{mark optional}} {{ttb|for (}} {{spar|init-statement}}{{mark optional}}{{spar|range_declaration}} {{ttb|:}} {{spar|range_expression}} {{ttb|)}} {{spar|loop_statement}} }} |
{{sdsc end}} | {{sdsc end}} | ||
Line 20: | Line 20: | ||
{{par | {{spar|range_expression}} | any {{rlp|expressions|expression}} that represents a suitable sequence (either an array or an object for which {{tt|begin}} and {{tt|end}} member functions or free functions are defined, see below) or a {{rlp|list initialization|braced-init-list}}. }} | {{par | {{spar|range_expression}} | any {{rlp|expressions|expression}} that represents a suitable sequence (either an array or an object for which {{tt|begin}} and {{tt|end}} member functions or free functions are defined, see below) or a {{rlp|list initialization|braced-init-list}}. }} | ||
{{par | {{spar|loop_statement}} | any {{rlp|statements|statement}}, typically a compound statement, which is the body of the loop }} | {{par | {{spar|loop_statement}} | any {{rlp|statements|statement}}, typically a compound statement, which is the body of the loop }} | ||
− | |||
{{par end}} | {{par end}} | ||
Line 62: | Line 61: | ||
:{{spar|init-statement}}<br> | :{{spar|init-statement}}<br> | ||
:{{ttb|auto && __range {{=}}}} {{spar|range_expression}} {{ttb|;}} <br> | :{{ttb|auto && __range {{=}}}} {{spar|range_expression}} {{ttb|;}} <br> | ||
− | :{{ttb|auto __begin {{=}} | + | :{{ttb|auto __begin {{=}}}} ''begin_expr'' {{ttb|;}}<br> |
:{{ttb|auto __end {{=}}}} ''end_expr'' {{ttb|;}}<br> | :{{ttb|auto __end {{=}}}} ''end_expr'' {{ttb|;}}<br> | ||
− | :{{ttb|for ( ; __begin !{{=}} __end; | + | :{{ttb|for ( ; __begin !{{=}} __end; ++__begin) {}} <br> |
::{{spar|range_declaration}} {{ttb|{{=}} *__begin;}} <br> | ::{{spar|range_declaration}} {{ttb|{{=}} *__begin;}} <br> | ||
::{{spar|loop_statement}} <br> | ::{{spar|loop_statement}} <br> |
Revision as of 16:02, 21 March 2019
Executes a for loop over a range.
Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.
Contents |
Syntax
attr(optional) for ( range_declaration : range_expression ) loop_statement
|
|||||||||
attr(optional) for ( init-statement(optional)range_declaration : range_expression ) loop_statement
|
(since C++20) | ||||||||
attr | - | any number of attributes |
init-statement(C++20) | - | either
|
range_declaration | - | a declaration of a named variable, whose type is the type of the element of the sequence represented by range_expression, or a reference to that type. Often uses the auto specifier for automatic type deduction |
range_expression | - | any expression that represents a suitable sequence (either an array or an object for which begin and end member functions or free functions are defined, see below) or a braced-init-list.
|
loop_statement | - | any statement, typically a compound statement, which is the body of the loop |
range_declaration may be a structured binding declaration for (auto&& [first,second] : mymap) { // use first and second } |
(since C++17) |
Explanation
The above syntax produces code equivalent to the following (__range
, __begin
and __end
are for exposition only):
|
(until C++17) |
|
(since C++17) |
|
(since C++20) |
range_expression is evaluated to determine the sequence or range to iterate. Each element of the sequence, in turn, is dereferenced and assigned to the variable with the type and name given in range_declaration.
begin_expr
and end_expr
are defined as follows:
- If range_expression is an expression of array type, then
begin_expr
is __range andend_expr
is (__range + __bound), where__bound
is the number of elements in the array (if the array has unknown size or is of an incomplete type, the program is ill-formed) - If range_expression is an expression of a class type
C
that has both a member named begin and a member named end (regardless of the type or accessibility of such member), thenbegin_expr
is __range.begin() andend_expr
is __range.end(); - Otherwise,
begin_expr
is begin(__range) andend_expr
is end(__range), which are found via argument-dependent lookup (non-ADL lookup is not performed).
If range_expression returns a temporary, its lifetime is extended until the end of the loop, as indicated by binding to the forwarding reference __range
, but beware that the lifetime of any temporary within range_expression is not extended.
This problem may be worked around using init-statement: for (auto& x : foo().items()) { /* .. */ } // undefined behavior if foo() returns by value for (T thing = foo(); auto& x : thing.items()) { /* ... */ } // OK |
(since C++20) |
Just as with a traditional loop, a break statement can be used to exit the loop early and a continue statement can be used to restart the loop with the next element.
attr represents an optional number of attributes.
Notes
If the initializer (range_expression) is a braced-init-list, __range is deduced to be std::initializer_list<>&&
It is safe, and in fact, preferable in generic code, to use deduction to forwarding reference, for (auto&& var : sequence).
The member interpretation is used if the range type has a member named begin
and a member named end
. This is done regardless of whether the member is a type, data member, function, or enumerator, and regardless of its accessibility. Thus a class like class meow { enum { begin = 1, end = 2}; /* rest of class */ }; cannot be used with the range-based for loop even if the namespace-scope begin/end functions are present.
While the variable declared in the range_declaration is usually used in the loop_statement, doing so is not required.
As of C++17, the types of the begin_expr
and the end_expr
do not have to be the same, and in fact the type of the end_expr
does not have to be an iterator: it just needs to be able to be compared for inequality with one. This makes it possible to delimit a range by a predicate (e.g. "the iterator points at a null character").
When used with a (non-const) object that has copy-on-write semantics, the range-based for loop may trigger a deep copy by (implicitly) calling the non-const begin()
member function. If that is undesirable (for instance because the loop is not actually modifying the object), std::as_const can be used:
struct cow_string { /* ... */ }; // a copy-on-write string cow_string str = /* ... */; // for(auto x : str) { /* ... */ } // may cause deep copy for(auto x : std::as_const(str)) { /* ... */ }
Keywords
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
P0962R1 | C++11 | member interpretation is used if either member begin and end is present | only used if both are present |
Example
#include <iostream> #include <vector> int main() { std::vector<int> v = {0, 1, 2, 3, 4, 5}; for (const int& i : v) // access by const reference std::cout << i << ' '; std::cout << '\n'; for (auto i : v) // access by value, the type of i is int std::cout << i << ' '; std::cout << '\n'; for (auto&& i : v) // access by forwarding reference, the type of i is int& std::cout << i << ' '; std::cout << '\n'; const auto& cv = v; for (auto&& i : cv) // access by f-d reference, the type of i is const int& std::cout << i << ' '; std::cout << '\n'; for (int n : {0, 1, 2, 3, 4, 5}) // the initializer may be a braced-init-list std::cout << n << ' '; std::cout << '\n'; int a[] = {0, 1, 2, 3, 4, 5}; for (int n : a) // the initializer may be an array std::cout << n << ' '; std::cout << '\n'; for (int n : a) std::cout << 1 << ' '; // the loop variable need not be used std::cout << '\n'; }
Output:
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 1 1 1 1 1 1
See also
applies a function to a range of elements (function template) |