An expansion statement S is equivalent to a compound-statement
containing instantiations of the for-range-declaration
(including its implied initialization),
together with the compound-statement of S, as follows:
Otherwise, if S is an iterating expansion statement, S is equivalent to:
{init-statementconstexproptdecltype(auto)range=(expansion-initializer);
constexproptautobegin=begin-expr; // see [stmt.ranged]S0
⋮
SN−1}
where N is the result of evaluating the expression
[&]consteval{
std::ptrdiff_t result =0;
auto b =begin-expr;
auto e =end-expr;
for(; b != e; ++b)++result;
return result; // distance from begin to end}()
and Si is
{constexproptautoiter=begin+decltype(begin - begin){i};
for-range-declaration=*iter;
compound-statement}
The variables range, begin, and iter
are defined for exposition only.
[Example 3: struct S {int i;
short s;
};
constevallong f(S s){long result =0;
templatefor(auto x : s){// OK, destructuring expansion statement
result +=sizeof(x);
}return result;
}static_assert(f(S{})==sizeof(int)+sizeof(short));
— end example]