Partial class specialization for function pointer type and value

时光毁灭记忆、已成空白 提交于 2019-12-11 10:40:57

问题


I'm using FLTK to do my GUI related stuff, and it requires functions of type void (*fn)( Fl_Widget*, void* ) to be registered as widget callbacks. I'm tired of creating function forwarders by hand that unpack the void*s into parameters and call appropriate static functions from my classes to do the work the user has requested.

I came up with a solution, but it requires a class to be specialized for both function type, and function address, and this is where I'm having problems. Here's some code:

template< typename T, typename fn_ptr_t, fn_ptr_t fn_ptr >
struct Fl_Callback_package;

template< typename T, typename Return, typename... Params >
struct Fl_Callback_package< T, Return (*)( Params... ), /* What goes here? */ >
{
  //...
};

EDIT: To clarify - I don't want a specific function in place of /* What goes here? */, but rather I would like this parameter to be Return (*fn_ptr)( Params... ). When I try

template< typename T, typename Return, typename... Params >
struct Fl_Callback_package< T, Return (*)( Params... ), Return (*fn_ptr)( Params... ) >

I get an error from GCC 4.8.1 saying that fn_ptrwas not declared in this scope.


回答1:


You put Return (*fn_ptr)( Params... ) in the wrong place. It's a template parameter of the partial specialization, so it goes into the template <...>.

template< typename T, typename fn_ptr_t, fn_ptr_t fn_ptr >
struct Fl_Callback_package;

template< typename T, typename Return, typename... Params, Return (*fn_ptr)( Params... ) >
struct Fl_Callback_package< T, Return (*)( Params... ), fn_ptr >
{
  //...
};


来源:https://stackoverflow.com/questions/28792422/partial-class-specialization-for-function-pointer-type-and-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!