Sequencing among a variadic expansion

亡梦爱人 提交于 2019-12-21 15:39:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/10764867/sequencing-among-a-variadic-expansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!