[英]C++ function wrapper of a function with a template argument
I am trying to make a function wrapper that takes a function with 1 template argument and use it in another header file. 我正在尝试制作一个带有1个模板参数的函数的函数包装器,并在另一个头文件中使用它。 Basically the main program computes certain variables that defines my_function, which is used to define a "criteria.h" header in CGAL. 基本上,主程序会计算一些定义my_function的变量,这些变量用于在CGAL中定义“ criteria.h”标头。 Here is "sizing_fun.h" that contains the function and the function wrapper: 这是“ sizing_fun.h”,其中包含函数和函数包装器:
template <typename Point_vec>
double my_function(double x, double y, Point_vec list_of_points)
{
//use list_of_points
return 4.0+(x*x+y*y);
}
template <typename FT, typename Point_2, typename Point_vec>
class FT_to_point_function_wrapper : public std::binary_function<Point_2, Point_vec, FT>
{
typedef FT (*Implicit_function)(FT, FT, FT);
Implicit_function function;
public:
FT_to_point_function_wrapper(Implicit_function f) : function(f) {}
FT operator()(Point_2 p, Point_vec list) const { return function(p.x(), p.y(), std::forward<Point_vec>(list)); } //error line
};
In the "criteria.h", I use my_func as the function wrapper defined above. 在“ criteria.h”中,我将my_func用作上面定义的函数包装器。 pcc is the Point_2 argument, and my_list is the Point_vec argument. pcc是Point_2参数,而my_list是Point_vec参数。
double local_squared_size_bound = my_func(pcc,my_list);
I go error messages: 我走了错误消息:
sizing_fun.h:17: error: cannot convert 'std::vector<CGAL::Point_2<CGAL::Epick>, std::allocator<CGAL::Point_2<CGAL::Epick> > >' to 'double' in argument passing
So it looks like the Point_vec type is not passed along correctly. 因此,看起来Point_vec类型没有正确传递。
I realized this post: C++ function call wrapper with function as template argument But I think it is different because its functions do not have a template argument. 我意识到了这篇文章: 以函数作为模板参数的C ++函数调用包装器但是我认为这是不同的,因为其函数没有模板参数。
typedef FT (*Implicit_function)(FT, FT, FT);
You declared that the function accepts the same type for all 3 parameters, and also returns the very same type. 您声明该函数对所有3个参数接受相同的类型,并且还返回完全相同的类型。
Should have been typedef FT (*Implicit_function)(FT, FT, Point_vec);
应该是typedef FT (*Implicit_function)(FT, FT, Point_vec);
Fix the signature of Implicit_function
, and your problem should be gone. 修复Implicit_function
的签名,您的问题应该消失了。
In case this is at least C++11, you should also be preferring std::function
over raw function pointer so that functions or lambdas with bindings / captures can be accepted. 如果至少是C ++ 11,则还应该首选std::function
不是原始函数指针,以便可以接受带有绑定/捕获的函数或lambda。
FT_to_point_function_wrapper::function
should be declared const
as it is only set by initializer list in constructor. FT_to_point_function_wrapper::function
应该声明为const
因为它仅由构造函数中的初始化程序列表设置。 If using C++11, you may then also declare FT_to_point_function_wrapper::FT_to_point_function_wrapper
to be constexpr
. 如果使用C ++ 11,则还可以将FT_to_point_function_wrapper::FT_to_point_function_wrapper
声明为constexpr
。
FT_to_point_function_wrapper::operator()
should be declared to be const
as well. FT_to_point_function_wrapper::operator()
也应声明为const
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.