简体   繁体   English

使用计数器展开参数包

[英]Expand a parameter pack with a counter

I want to expand a paramter pack into a function call, like this: 我想将一个参数包扩展为函数调用,如下所示:

func(get_value(arg)...);

where func and get_value are two functions. 其中func和get_value是两个函数。 The question is that get_value is sensitive to evaluation order, so I think one solution is to generate something like this: 问题是get_value对评估顺序很敏感,所以我认为一个解决方案是生成这样的东西:

func(get_value(arg0, 0), get_value(arg1, 1), get_value(arg2, 2));

if, for example, there're three parameters. 例如,如果有三个参数。 With this counter I'll know the evaluation order. 有了这个计数器,我就会知道评估顺序。 This there a way to do so? 这有办法吗?

Or as another solution, is there a way to simply specify the evaluation order of those calls to get_value with args? 或者作为另一种解决方案,有没有办法简单地用args指定那些调用get_value的评估顺序? (If so, I don't even need a counter.) (如果是这样,我甚至不需要柜台。)

You can "zip" parameter packs together. 您可以将参数包“压缩”在一起。 So, something like this, using the definition of seq from here : 所以,像这样,使用seq的定义从这里

template <typename ... Args, int ... N>
void F(Args... args, seq<N...>)
{
func(get_value(args, N)...);
}

template <typename ... Args>
void FHelper(Args&&... args)
{
 F(std::forward<Args>(args)..., typename gens<sizeof...(Args)>::type ());
}

and you would call FHelper . 你会打电话给FHelper

A simpler solution, in the case of get_value having a fixed return type would be to change func to take an initializer_list as input and call it like this : 一个更简单的解决方案,在get_value具有固定返回类型的情况下,将更改func以将initializer_list作为输入并像这样调用它:

func({get_value(args)...});

This preserves call order since the commas in the initializer-list are sequence points. 这保留了调用顺序,因为初始化列表中的逗号是序列点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM