可变扩展之间的排序 [英] Sequencing among a variadic expansion

查看:93
本文介绍了可变扩展之间的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于这个非可变的例子:

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

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

$ c> Func1()或首先计算Func2(),只是两者必须在 MyFunc



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

c> f 是一个函数对象,在第一次调用后改变其状态。为 a 的每个段调用 f ?换句话说,将在 a 的列表中的第一个项目上调用 f ,然后第二个项目第三等,而不是随机跳过扩展列表?

解决方案

是的,括号括号初始化列表保证左侧的求值顺序向右,而函数调用不。



维基百科文章涵盖了这一点: https://en.wikipedia.org/wiki/Variadic_templates


是有什么我们用来调用每个项目之间的序列点?


不,而使用逗号令牌不是 逗号运算符。


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?

解决方案

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.

这篇关于可变扩展之间的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆