如何使用模板专门化来查找成员函数参数类型等?
我确信我以前见过这种描述,但现在我一辈子都找不到它。
给定一个具有某种形式的成员函数的类,例如:
int Foo::Bar(char, double)
如何使用模板和各种专业化来推导构成类型,例如:
template<typename Sig>
struct Types;
// specialisation for member function with 1 arg
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0)>
{
typedef RetType return_type;
typedef ClassType class_type;
typedef MemFunc mem_func;
typedef Arg0 argument_0;
etc...
};
// specialisation for member function with 2 args
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0, Arg1)>
{
typedef RetType return_type;
typedef ClassType class_type;
typedef MemFunc mem_func;
typedef Arg0 argument_0;
typedef Arg0 argument_1;
etc...
};
这样当我使用上述成员函数实例化类型时,例如:
Types<&Foo::Bar>
它解析为正确的专业化,并声明相关的 typedef?
编辑:
我正在使用快速委托,并将回调静态绑定到成员函数。
我有以下模型,我相信它会静态绑定到成员函数:
#include <iostream>
template<class class_t, void (class_t::*mem_func_t)()>
struct cb
{
cb( class_t *obj_ )
: _obj(obj_)
{ }
void operator()()
{
(_obj->*mem_func_t)();
}
class_t *_obj;
};
struct app
{
void cb()
{
std::cout << "hello world\n";
}
};
int main()
{
typedef cb < app, &app::cb > app_cb;
app* foo = new app;
app_cb f ( foo );
f();
}
但是 - 如何以上述方式将其作为专业化?
I'm sure I've seen this described before but can't for the life of me find it now.
Given a class with a member function of some form, eg:
int Foo::Bar(char, double)
How can I use a template and various specialisations to deduce the constituent types, eg:
template<typename Sig>
struct Types;
// specialisation for member function with 1 arg
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0)>
{
typedef RetType return_type;
typedef ClassType class_type;
typedef MemFunc mem_func;
typedef Arg0 argument_0;
etc...
};
// specialisation for member function with 2 args
template<typename RetType, typename ClassType, etc...>
struct Types<RetType (ClassType::*MemFunc)(Arg0, Arg1)>
{
typedef RetType return_type;
typedef ClassType class_type;
typedef MemFunc mem_func;
typedef Arg0 argument_0;
typedef Arg0 argument_1;
etc...
};
Such that when I instantiate Types with my above member function, eg:
Types<&Foo::Bar>
it resolves to the correct specialisation, and will declare the relevant typedefs?
Edit:
I'm playing around with fast-delegates with the callback statically bound to a member function.
I have the following mockup which I believe does statically bind to the member function:
#include <iostream>
template<class class_t, void (class_t::*mem_func_t)()>
struct cb
{
cb( class_t *obj_ )
: _obj(obj_)
{ }
void operator()()
{
(_obj->*mem_func_t)();
}
class_t *_obj;
};
struct app
{
void cb()
{
std::cout << "hello world\n";
}
};
int main()
{
typedef cb < app, &app::cb > app_cb;
app* foo = new app;
app_cb f ( foo );
f();
}
However - how to get this as a specialisation in the manner above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎已经得到它,除了额外的
MemFunc
,它不是类型的一部分。然而,您不能使用,
因为 Foo::Bar 是一个成员函数指针,而不是它的类型。您需要一些编译器扩展来获取 C++03 中的类型,例如
gcc 中的 typeof
或 Boost.Typeof:或升级到 C++11 并使用标准
decltype
:You've almost got it, except that extra
MemFunc
, which is not part of the type.Nevertheless, you cannot use
because Foo::Bar is a member function pointer, not the type of it. You'll need some compiler extensions to get the type in C++03, e.g.
typeof
in gcc or Boost.Typeof:or upgrade to C++11 and use the standard
decltype
: