template function作为模板函数的参数 - 未解析的函数类型错误

时间:2015-05-30 13:30:59

标签: c++ templates eigen

我试图将接收到对Eigen MatrixBase的引用的函数模板F_1传递给另一个函数模板F_2,然后用它做一些事情:

//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函数调用中收到错误“unresolved overloaded function type”:

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;
}
  1. 如何让编译器推导出F_2(模板参数f)中“包裹”的类型?
  2. 你有什么建议可以用Eigen做不同的事吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

F_2(F_1_broken<double>,x,out);

编译器无法在此处推断出类型。 所以你必须帮助他: - )

相关问题