0
我试图传递的接收的参考到本征MatrixBase到另一个函数模板F_2那将,用它做什么函数模板F_1:模板函数作为参数传递给模板函数 - 悬而未决的功能类型错误
//Function that will be argument to F_2
template<typename derived_matrix,typename derived_float>
void F_1(const MatrixBase<derived_matrix>& x,derived_float t,MatrixBase<derived_matrix>& store){
store = 2*x;
}
template<typename derivative_function,typename const_derived_matrix,typename derived_matrix,typename derived_float>
void F_2(derivative_function derivatives,const_derived_matrix x0,derived_float t0,derived_float dt,derived_matrix& output){
//Do something with F_1(= derivatives)
derived_matrix k1;
derived_matrix k2;
derived_matrix k3;
derived_matrix k4;
derivatives(x0,t0,k1);
derivatives(x0+dt*k1/2,t0+dt/2,k2);
derivatives(x0+dt*k2/2,t0+dt/2,k3);
derivatives(x0+dt*k3/2,t0+dt,k4);
output = x0+dt*(k1+2*k2+2*k3+k4)/6;
}
但我从F_2函数调用收到错误“未解决重载函数类型”:
double t = 0;
double dt = 0.1;
Vector2d x;
x << 1,2;
Vector2d out_value;
F_2(F_1,x,t,dt,out_value); //unresolved overloaded function type
它减少到最低程度,我只能做一个普通的F_2模板函数接收F_1功能时,它(F_1 )不是模板:
#include <iostream>
#include <functional>
using namespace std;
template<typename f,typename derived_float>
void F_2(f wrapped,derived_float x1,derived_float& x2){
wrapped(x1,x2);
}
void F_1_works(double& x,double& store_output){
store_output = 2*x;
}
template<typename T>
void F_1_broken(double& x,T& store_output){
store_output = 2*x;
}
int main(){
double out= 0;
double x = 25;
F_2(F_1_works,x,out); //compiles!
F_2(F_1_broken,x,out); //Error:
// no matching function for call to ‘F_2(<unresolved overloaded function type>, double&, double&)’
// couldn't deduce template parameter ‘f’
cout<<out<<endl;
return 0;
}
- 我怎样才能让编译器推断的“包装”在F_2(模板参数f)的类型?
- 您有什么建议可以与Eigen做不同吗?
'F_1'是一个模板函数,编译器不能简单地推导出模板参数(即,你必须'F_2(F_1 ?????>,x,t, DT,out_value);')。 – 101010
是否有可能使它从函数实例化中推导出类型? – maz