[英]multiple specializations of a variadic template with a statically bound member function pointer?
Is it possible to have multiple specializations of a variadic template where one of the template parameters is a statically bound member function pointer? 可变参数模板是否可能具有多个特化,其中模板参数之一是静态绑定的成员函数指针?
I'm attempting to build a delegate where the callback function is a compile time constant - thereby aiding the optimizer to see past the function pointer boundary. 我试图建立一个委托,其中回调函数是一个编译时间常数-从而帮助优化程序查看超出函数指针边界的位置。
I have the following code where I pass a member function pointer as a template parameter, and since the function pointer is a constant which is known at compile-time, my expectation is that the optimizer will be able to work through the function pointer boundary. 我有以下代码,在该代码中我将成员函数指针作为模板参数传递,并且由于函数指针是在编译时已知的常量,因此我期望优化器将能够通过函数指针边界工作。
I have created 2 delegates, delegate0 and delegate1, which are for member functions which have 0 and 1 arguments respectively. 我创建了2个委托,delegation0和委托1,分别用于具有0和1参数的成员函数。
#include <iostream>
template<class class_t, void (class_t::*mem_func_t)()>
struct delegate0
{
delegate0( class_t *obj_ )
: _obj(obj_)
{ }
void operator()()
{
(_obj->*mem_func_t)();
}
private:
class_t *_obj;
};
template<class class_t, typename arg0, void (class_t::*mem_func_t)(arg0)>
struct delegate1
{
delegate1( class_t *obj_, arg0 a0_ )
: _obj(obj_)
, _a0(a0_)
{ }
void operator()()
{
(_obj->*mem_func_t)(_a0);
}
private:
class_t *_obj;
arg0 _a0;
};
struct app
{
void cb()
{
std::cout << "hello world\n";
}
void cb1(int i)
{
std::cout << "hello world " << i << "\n";
}
};
int main()
{
app* foo = new app;
delegate0<app, &app::cb> f(foo);
f();
delegate1<app, int, &app::cb1> f1(foo, 5);
f1();
}
However, I would like to improve on this in 2 ways: 但是,我想通过两种方式对此进行改进:
delegate<&app::cb>
(when cb is not ambiguous), class_t, mem_func_t, arg0, arg1, etc... are all deduced from the signature for app::cb
. delegate<&app::cb>
(当cb不明确时),class_t,mem_func_t,arg0,arg1等的东西都从app::cb
的签名推导出。 I realize that a member function pointer is not a type, but just like you can pass a particular integer as a template parameter (ala template recursion used in metaprogramming), I figure you can have a specific member function pointer as a parameter - thereby allowing static binding to that function. 我意识到成员函数指针不是类型,但是就像您可以将特定的整数作为模板参数(元编程中使用的ala模板递归)一样,我认为您可以将特定的成员函数指针作为参数-从而允许静态绑定到该功能。
Is what I'm after even possible? 我什至有可能吗? If not, is either of 1 or 2 above possible?
如果不是,是否可以使用以上1或2? I would really appreciate a working example, because I've been banging my head against my keyboard with no success as of yet.
我会很感激一个可行的示例,因为我一直在用键盘敲打头,但至今没有成功。
I have the following miserable attempt. 我有以下惨痛的尝试。 It is clearly not what I'm looking for, but in order to show the direction I've been heading, I thought it perhaps useful to include.
显然,这并不是我要找的东西,但是为了显示我一直在前进的方向,我认为包括进去可能有用。
template<typename...>
struct delegate;
template<class class_t, void (class_t::*mem_func_t)()>
struct delegate<class_t, decltype(mem_func_t)>
{
delegate( class_t *obj_ )
: _obj(obj_)
{ }
void operator()(mem_func_t f)
{
(_obj->*f)();
}
class_t *_obj;
};
template<class class_t, typename arg0, void (class_t::*mem_func_t)(arg0)>
struct delegate<class_t, arg0, decltype(mem_func_t)>
{
delegate( class_t *obj_, arg0 a0_ )
: _obj(obj_)
, _a0(a0_)
{ }
void operator()()
{
(_obj->*mem_func_t)(_a0);
}
class_t *_obj;
arg0 _a0;
};
Declare a template taking any types: 声明采用任何类型的模板:
template <typename T, T value>
struct Delegate;
and then specialize it for member function objects (do it 4 times for each cv-qualifier): 然后将其专门用于成员函数对象(对每个cv限定符执行4次):
template <typename R, typename C, typename... A, R (C::* value)(A...) const>
struct Delegate<R(C::*)(A...) const, value>
{
// do whatever you like with R, C, A... .
};
As I've answered before, you'll need decltype
: 正如我之前回答的那样,您将需要
decltype
:
Delegate<decltype(&SomeClass::method), &SomeClass::method> del;
Alternatively, you could use my function_traits
class which can extract the R, C and A... from T directly so you don't need to specialize, but decltype
and repeating the method is still needed. 另外,您可以使用我的
function_traits
类 , 该类可以直接从T中提取R,C和A ...,因此您不需要专门化,但是仍然需要decltype
和重复方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.