C ++ 11: Variadic Template Function Parameter Pack Extension execution order

Consider the following code:

template<class T>
size_t f(T t, size_t& x) { return x++; }

template<class... Args>
void g(Args... args)
{
    size_t x = 0;
    size_t y[] = { f(args, x)... };

    for (size_t i = 0; i < sizeof...(args); i++)
        assert(y[i] == i);
}

Is the assertion guaranteed by the C ++ 11 standard not work? Why or why not?

+5
source share
1 answer

Yes, it is guaranteed not to work. See the following citations:

ยง14.5.3 Variation patterns:

Package extensions may occur in the [...] list of initializers; initializer sentence template.

ยง8.5.1 Units:

The complete expressions in the initializer clause are evaluated in the order in which they appear.

+7
source

All Articles