模板函数作为模板函数的参数 - 未解析的函数类型错误
template function as argument to template function - unresolved function type error
我试图将接收到 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;
}
- 如何让编译器在F_2(模板参数f)中推导出"wrapped"的类型?
- 对于使用 Eigen 做不同的事情,您有什么建议吗?
试试这个:
F_2(F_1_broken<double>,x,out);
编译器无法推断出这里的类型。
所以你必须帮助他:-)
我试图将接收到 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;
}
- 如何让编译器在F_2(模板参数f)中推导出"wrapped"的类型?
- 对于使用 Eigen 做不同的事情,您有什么建议吗?
试试这个:
F_2(F_1_broken<double>,x,out);
编译器无法推断出这里的类型。 所以你必须帮助他:-)