Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequencing among a variadic expansion

For this non-variadic example:

int     Func1();
double  Func2();
void    MyFunc( int, double );

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

it's not specified whether Func1() or Func2() is computed first, just that both must be done before MyFunc() is called.

How does this sequencing work with the expansion of variadic arguments?

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

Let's say that f is a function object that changes its state after its first call. Will f be called in order for each segment of a? In other words, will f be called on the first item in a's list, then the second item, the third, etc., instead of randomly skipping through the expanded list? Is there what we used to call sequence points between each item?

like image 430
CTMacUser Avatar asked May 26 '12 08:05

CTMacUser


1 Answers

Yes, brace enclosed initializer lists guarantee an evaluation order of left-to-right, while function calls do not. So MyFunc2 will sequence correctly.

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

Is there what we used to call sequence points between each item?

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

like image 68
Pubby Avatar answered Nov 07 '22 14:11

Pubby