作为模板函数参数的模板函数-未解析的函数类型错误
template function as argument to template function - unresolved function type error
我试图将一个函数模板F_1传递给另一个函数模板F_2,该模板接收到对Eigen MatrixBase的引用,然后,用它做一些事情:
//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,你有什么不同的建议吗?
try this:
F_2(F_1_broken<double>,x,out);
编译器无法推断这里的类型。所以你得帮帮他:-)
相关文章:
- 为什么在我的函数类型后使用引用运算符 (&) 允许我修改它返回的值?
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 是否有任何建议来统一函数类型限定符并简化可恶的函数类型?
- 关于 C++ 中的函数类型定义
- 用于检测函数类型是否为否的特征
- 函数类型参数的模板参数推导
- 标准对此指向成员函数类型模板参数有何说明?是我的代码有误,还是 MSVS 16.6 有问题?
- C++无效的函数类型转换
- STL 函数和函数类型与函数指针类型
- 如何将result_of与函数类型定义一起使用
- 将模板(没有规范)传递给 std::thread() 会出现错误:<未解析的重载函数类型>匹配错误
- C++ 编译错误:gnu_printf是无法识别的格式函数类型
- 专门用于"direct"函数类型(与函数指针类型相对)
- 将函数类型作为模板参数传递不会编译
- 通过参数传递 lambda(无函数类型模板)
- 如何在模板参数中分离函数类型返回类型和参数
- 为什么比较函数类型需要指定为模板参数?
- 带有限定符的函数类型定义用例
- 如何声明对函数类型的常量引用
- 非类型模板参数允许各种函数类型?