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-statementconstexprauto&range=expansion-initializer;
constexprautobegin=begin-expr; / see [stmt.ranged]constexprautoend=end-expr; / see [stmt.ranged]S0
⋮
SN−1}
where N is the result of evaluating the expression
[]consteval{
std::ptrdiff_t result =0;
for(auto i =begin; i !=end; ++i)++result;
return result; / distance from begin to end}()
and Si is
{constexprautoiter=begin+decltype(begin - begin){i};
for-range-declaration=*iter;
compound-statement}
The variables range, begin, end, and iter
are defined for exposition only.
The identifier i is considered to be
a prvalue of type std::ptrdiff_t;
the program is ill-formed if
i is not representable as such a value.
[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]