函数指针类型和值的部分类专门化 [英] Partial class specialization for function pointer type and value

查看:88
本文介绍了函数指针类型和值的部分类专门化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FLTK来完成与GUI相关的工作,并且它要求将 void(* fn)(Fl_Widget *,void *)类型的函数注册为小部件回调。我厌倦了手工创建函数转发器,将 void * s解压缩为参数并从类中调用适当的静态函数来完成用户请求的工作。

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? */ >
{
  //...
};

编辑:
为了澄清-我不想用特定的功能代替 / *这里是什么? * / ,但我希望此参数为 Return(* fn_ptr)(Params ...)

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... ) >

我从GCC 4.8.1收到一个错误,说 fn_ptr

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

推荐答案

您将 Return(* fn_ptr)( Params ...)放在错误的位置。这是部分专业化的模板参数,因此它进入 template< ...>

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 >
{
  //...
};

这篇关于函数指针类型和值的部分类专门化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆