[英]C++ variadic function template in std::functional
基本上我想将多维积分与此递归集成。
但问题本身是一个普遍问题。 它不是特定于集成的。
#include "math.h"
#include <iostream>
#include <functional>
double f(double x,double y,double z){
return x+y+z+1;
}
//Base
double redDim(std::function<double(double)> &f){
return f(0); //a silly integrator for testing
}
// Recursion
template<typename Tfirst=double, typename... Trest>
auto redDim(std::function<double(Tfirst first,Trest... rest)> &f){
return redDim([=](Trest... R){return redDim([=](double x){return f(x,R...);});});
}
int main(){
std::cout<<redDim(f)<<std::endl;
return 0;
}
问题是,编译器说:
c:\C++\templateTutorial\templateTut.cpp: In function 'int main()':
c:\C++\templateTutorial\templateTut.cpp:24:19: error: no matching function for call to 'redDim(double (&)(double, double, double))'
cout<<redDim(f)<<endl;
^
c:\C++\templateTutorial\templateTut.cpp:12:8: note: candidate: 'double redDim(std::function<double(double)>&)'
double redDim(std::function<double(double)> &f){
^~~~~~
c:\C++\templateTutorial\templateTut.cpp:12:8: note: no known conversion for argument 1 from 'double(double, double, double)' to 'std::function<double(double)>&'
c:\C++\templateTutorial\templateTut.cpp:17:6: note: candidate: 'template<class Tfirst, class ... Trest> auto redDim(std::function<double(Tfirst, Trest ...)>&)'
auto redDim(std::function<double(Tfirst first,Trest... rest)> &f){
^~~~~~
c:\C++\templateTutorial\templateTut.cpp:17:6: note: template argument deduction/substitution failed:
c:\C++\templateTutorial\templateTut.cpp:24:19: note: mismatched types 'std::function<double(Tfirst, Trest ...)>' and 'double(double, double, double)'
cout<<redDim(f)<<endl;
^
The terminal process terminated with exit code: 1
那么为什么f
的类型不符合redDim()
的要求呢?
因此,我什至无法测试,如果我的方法有效。
我希望你能帮助我!
函数指针不是std::function
。
模板参数推导不做任何类型转换,除了一些 to-base 情况。
添加一个调用 std 函数 one 的redim( double(*f)(Args...) )
模板。
template<class...Args>
auto redDim( double(*f)(Args...) {
return redDim( std::function<double(Args...)>{ f } );
}
它应该工作。
这可以推导出函数指针的签名。 然后它显式转换为std::function
,然后匹配您的其他redim
函数。
您必须将其他 lambda 显式转换为std::function
:
// Recursion
template<typename Tfirst, typename... Trest>
auto redDim(std::function<double(Tfirst first,Trest... rest)> f){
return redDim(
std::function<double(Trest...)>{
[=](Trest... R){
return redDim(
std::function<double(double)>{
[=](double x){return f(x,R...);}
}
);
}
}
);
}
redDim
最后一个redDim
的参数更改为std::function<double(double)>
(不是参考)。 或者,一个const&
。
基于 Yakk - Adam Nevraumont 的回答,应该编译以下代码:
#include "math.h"
#include <iostream>
#include <functional>
double f(double x,double y,double z){
return x+y+z+1;
}
//Base
double redDim(const std::function<double(double)> &f){
return f(0); //a silly integrator for testing
}
// Recursion
template<typename Tfirst=double, typename... Trest>
auto redDim(const std::function<double(Tfirst first,Trest... rest)> &f) {
return redDim(
std::function<double(Trest...)>{
[=](Trest... R)->double{return redDim([=](double x){return f(x,R...);});}});
}
template<class...Args>
auto redDim( double(*f)(Args...)) {
return redDim( std::function<double(Args...)>{ f } );
}
int main(){
std::cout<<redDim(f)<<std::endl;
return 0;
}
正如 Yakk 已经指出的那样, std::function
和函数指针没有相同的类型。 请注意,我还将redDim
的参数类型redDim
为const std::function<...> &)
。 您还可以使用&&
语法传递rvalue
引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.