如何使用模板专门化来查找成员函数参数类型等? [英] How can I use template specialisation to find member function argument types etc?
问题描述
我确定我已经看过这个描述,但不能为我的生活找到它现在。例如:
int Foo :: Bar(char,double)
如何使用模板和各种专业化来推导构成类型,例如:
template< typename Sig>
struct Types;
//具有1个arg的成员函数的特殊化
template< typename RetType,typename ClassType等...>
struct Types< RetType(ClassType :: * MemFunc)(Arg0)>
{
typedef RetType return_type;
typedef ClassType class_type;
typedef MemFunc mem_func;
typedef Arg0 argument_0;
etc ...
};
//具有2个args的成员函数的特殊化
template< typename RetType,typename ClassType等...>
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?
编辑:
我正在玩快速委托,回调静态绑定到一个成员函数。
我有以下的mockup,我相信它静态绑定到成员函数:
#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\\\
;
}
};
int main()
{
typedef cb< app,& app :: cb> app_cb;
app * foo = new app;
app_cb f(foo);
f();
}
然而 - 如何以上述方式将其作为专业化? p>
你几乎有了,除了额外 MemFunc
不是类型的一部分。
模板< typename RetType,typename ClassType,typename Arg0>
struct Types< RetType(ClassType :: *)(Arg0)> //< - 无MemType
{
typedef RetType return_type;
typedef ClassType class_type;
// typedef MemFunc mem_func; //< - 删除此行
typedef Arg0 argument_0;
}
无论如何, > use
类型<& Foo :: Bar>
因为Foo :: Bar是一个成员函数指针,而不是它的类型。你需要一些编译器扩展来获得C ++ 03中的类型,例如。 gcc中的 typeof
或 Boost.Typeof :
Types< typeof(& Foo :: Bar)>
或升级到C ++ 11并使用标准 decltype
:
Types< decltype(& Foo :: Bar)>
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?
You've almost got it, except that extra MemFunc
, which is not part of the type.
template<typename RetType, typename ClassType, typename Arg0>
struct Types<RetType (ClassType::*)(Arg0)> // <-- no MemType
{
typedef RetType return_type;
typedef ClassType class_type;
// typedef MemFunc mem_func; // <-- remove this line
typedef Arg0 argument_0;
};
Nevertheless, you cannot use
Types<&Foo::Bar>
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:
Types<typeof(&Foo::Bar)>
or upgrade to C++11 and use the standard decltype
:
Types<decltype(&Foo::Bar)>
这篇关于如何使用模板专门化来查找成员函数参数类型等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!