簡體   English   中英

具有模板參數的函數的C ++函數包裝器

[英]C++ function wrapper of a function with a template argument

我正在嘗試制作一個帶有1個模板參數的函數的函數包裝器,並在另一個頭文件中使用它。 基本上,主程序會計算一些定義my_function的變量,這些變量用於在CGAL中定義“ criteria.h”標頭。 這是“ 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
};

在“ criteria.h”中,我將my_func用作上面定義的函數包裝器。 pcc是Point_2參數,而my_list是Point_vec參數。

double local_squared_size_bound = my_func(pcc,my_list);

我走了錯誤消息:

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

因此,看起來Point_vec類型沒有正確傳遞。

我意識到了這篇文章: 以函數作為模板參數的C ++函數調用包裝器但是我認為這是不同的,因為其函數沒有模板參數。

typedef FT (*Implicit_function)(FT, FT, FT);

您聲明該函數對所有3個參數接受相同的類型,並且還返回完全相同的類型。

應該是typedef FT (*Implicit_function)(FT, FT, Point_vec);

修復Implicit_function的簽名,您的問題應該消失了。

如果至少是C ++ 11,則還應該首選std::function不是原始函數指針,以便可以接受帶有綁定/捕獲的函數或lambda。

FT_to_point_function_wrapper::function應該聲明為const因為它僅由構造函數中的初始化程序列表設置。 如果使用C ++ 11,則還可以將FT_to_point_function_wrapper::FT_to_point_function_wrapper聲明為constexpr

FT_to_point_function_wrapper::operator()也應聲明為const

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM