简体   繁体   English

是否可以展开可变参数(lambda)模板并将这些函数的返回值传递给另一个可变参数函数?

[英]Is it possible to unfold a variadic (lambda) template and the pass those functions return value to another variadic function?

So I wonder given a variadic template function like following:所以我想知道给定一个可变参数模板函数,如下所示:

template<typename...Fs>
parse(int x, Fs...funcs);

Where we ensure (through C++20 concept) that is convertible to std::function<double(int)> .我们确保(通过 C++20 概念)可以转换为std::function<double(int)> Could we use unfold it into another functions argument, like passing to following one:我们可以将其展开为另一个函数参数,例如传递给以下参数:

template<typename...Ts>
test(Ts...args);

Where again we ensure all Ts is and must be able to convert double .我们再次确保所有Ts是并且必须能够转换double

What is expecting is, suppose a , b , c are lambda expressions, calling parse(12, a, b, c) is equivalent to calling test(a(12), b(12), c(12)) .期望的是,假设a , b , c是 lambda 表达式,调用parse(12, a, b, c)等效于调用test(a(12), b(12), c(12))


Something I tried is like following我试过的东西就像跟随

template<typename...Fs>
parse(int x, Fs...func) {
    return test(..., funcs(x));
}

But didn't work.但是没有用。 It seems like that most unfolding expression examples puts a function within the expression, so does this means unfold expression by itself can't return as a list of arguments.似乎大多数展开表达式示例都在表达式中放置了一个函数,这是否意味着展开表达式本身不能作为参数列表返回。 Instead, perhaps it could only return as single value?相反,也许它只能作为单个值返回?

If so, is there any workaround like first construct it into an array like (..., void(arr[i++]=args)) , but I couldn't find ways to expand it back to argument pack.如果是这样,是否有任何解决方法,例如首先将其构造为(..., void(arr[i++]=args))类的数组,但我找不到将其扩展回参数包的方法。 (I would want to expand arguments back to argument pack as I want to reuse other functions within my library instead of writing another one that takes either array or initializer list as argument.) (我想将参数扩展回参数包,因为我想重用库中的其他函数,而不是编写另一个将数组或初始化列表作为参数的函数。)

Syntax would be:语法是:

template <typename... Fs>
auto parse(int x, Fs... func)
{
    return test(funcs(x)...);
}

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

相关问题 是否可以从函数模板返回可变参数lambda? - Is it possible to return a variadic lambda from a function template? 是否可以将引用传递给可变参数模板函数? - Is it possible to pass a reference to a variadic template function? 变体模板函数接受lambda - Variadic template function accepting lambda 将“传递”函数模板作为generic-variadic lambda return语句的好方法是什么? - Is it good approach to “pass” function template as generic-variadic lambda return statement? 如何在可变参数模板类中将lambda表达作为参数传递给mermber函数 - how to pass lambda express as parameter into mermber function in variadic template class 如何正确地将C字符串传递给可变参数模板函数内的lambda - How to properly pass C strings to a lambda inside a variadic template function 将容器传递给可变参数模板 function - Pass container to a variadic template function 使用模板variadic函数将多个参数传递给另一个函数 - pass multiple arguments to another function using template variadic function Variadic lambda vs variadic template function:严格等效? - Variadic lambda vs variadic template function : stricly equivalent? 存储可变参数模板参数并将其传递给lambda - Store variadic template arguments and pass them to lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM