Below is from an authoritative C++ proposal:
template<class... TYPES>
constexpr void tuple<TYPES...>::swap(tuple& other)
{
for...(constexpr size_t N : view::iota(0uz, sizeof...(TYPES)))
{
swap(get<N>(*this), get<N>(other));
}
}
However, I cannot compile the following code in the same way:
#include <iostream>
#include <vector>
template<typename... Args>
void f(Args&&... args)
{
for...(auto n : args) // error: expected '(' before '...' token
{
std::cout << n << std::endl;
}
}
int main()
{
auto v = std::vector{1, 2, 3};
f(v, v, v);
}
See: https://godbolt.org/z/dEKsoqq8s
Why does the fold expression not apply to for
loop?
Because fold expressions are... expressions. A for loop is a statement. ...
unpacking applies (with one or two exceptions) to expressions, not to statements.