The sequence between variational expansion

For this non-invariant example:

int Func1(); double Func2(); void MyFunc( int, double ); int main() { MyFunc( Func1(), Func2() ); //... } 

it is not indicated whether Func1() or Func2() calculated first, only that both must be executed before calling MyFunc() .

How does this sequencing work with variable argument expansion?

 template < typename Func, typename ...Args > void MyFunc2( Func &&f, Args&& ...a ) { int b[] = { f( std::forward<Args>(a) )... }; //... } 

Let's say that f is an object of a function that changes its state after the first call. Call f for each segment a ? In other words, whether f will be called in the first element in list a , then the second element, third, etc. Instead of accidentally skipping through an extended list? Is there something we used to call the points of the sequence between each element?

+5
c ++ sequence-points c ++ 11 variadic-functions variadic-templates
May 26 '12 at 8:46 a.m.
source share
1 answer

Yes, copied initializer lists guarantee evaluation order from left to right, but function calls do not. This way, MyFunc2 will follow correctly.

The Wikipedia article covers this: https://en.wikipedia.org/wiki/Variadic_templates

Is there something we used to call the points of the sequence between each element?

No, while it uses a comma token, it is not a comma operator.

+5
May 26 '12 at 8:52
source share



All Articles