Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 可变模板C+中的多参数包+;_C++_Variadic Templates_Variadic Functions - Fatal编程技术网

C++ 可变模板C+中的多参数包+;

C++ 可变模板C+中的多参数包+;,c++,variadic-templates,variadic-functions,C++,Variadic Templates,Variadic Functions,我正在研究可变模板,我想知道我是否可以这样做:我想用任意数量和类型的参数调用任意数量的函数(任意返回类型)。我可以使用的代码如下: call(f1,f2,f3, 2.2,3); call(f4,f5, 2.2, "Hello", 'a'); 显然,如果传递的函数可以接受传递的参数。 我编写了一个版本,使用固定数量的参数传递给函数: template<typename T, typename U> void fcall(const T& t1, const T& t2

我正在研究可变模板,我想知道我是否可以这样做:我想用任意数量和类型的参数调用任意数量的函数(任意返回类型)。我可以使用的代码如下:

call(f1,f2,f3, 2.2,3);
call(f4,f5, 2.2, "Hello", 'a');
显然,如果传递的函数可以接受传递的参数。 我编写了一个版本,使用固定数量的参数传递给函数:

template<typename T, typename U>
void fcall(const T& t1, const T& t2, U&& u){
    u(t1, t2);
}

template<typename... F, typename T, typename U>
void fcall(const T& t1, const T& t2, U&& u, F&&... f){
    u(t1, t2);
    fcall(t1, t2, f...);
}
模板
无效fcall(常数T和t1、常数T和t2、U和U){
u(t1,t2);
}
模板
无效fcall(常数T&t1、常数T&t2、U&U、F&F){
u(t1,t2);
fcall(t1、t2、f.);
}
即使我不喜欢在函数之前传递参数,它也可以工作

这是我写的,但不起作用:

template<typename P, typename U>
void fcall2(U&& f, const P& param){
f(param);
}

template<typename... P, typename... F, typename U>
void fcall2(U&& f, F&&... func, const P&... param){
f(param...);
fcall2(func..., param...);
}
模板
无效fcall2(U&f、常量P¶m){
f(参数);
}
模板
无效fcall2(U&&f,f&&func,常量P&…参数){
f(参数…);
fcall2(函数…,参数;
}
我知道这没用,只是好奇而已。
有什么建议吗?

你只能有一个这样的包

理论上,您可以在包中找到唯一的拆分点,在使用了极其复杂的模板元编程之后,可以使用元素调用所有元素之前的元素

更容易写:

template<class...Fs>
auto call_all(Fs...fs){
  return [&](auto&&...args){
    (void(fs(args...)),...);
  };
}
call_all(f1,f2,f3)(3.13, "hello");