multiple specializations of a variadic template with a statically bound member function pointer?

348 views Asked by At

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.

#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:

  1. All permutations of the number of arguments to be specializations of a variadic delegate template.
  2. Use template argument deduction such that declaring something like 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.

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.

Is what I'm after even possible? If not, is either of 1 or 2 above possible? 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;
};
1

There are 1 answers

0
kennytm On BEST ANSWER

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

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:

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.