简体   繁体   中英

Defining a proxy for a member function that takes a member function pointer as a template argument

I am using a third-party library with a class that that is declared like this:

template<typename Ret, typename... Args>
class Delegate<Ret(Args...)>
{
  // ...
};

and it has some template member function that have 2 template parameters. The first parameter is the class type, and the second parameter is the member function pointer:

template<typename Class, Ret(Class:: *Member)(Args...) const>
void connect(Class *instance);

template<typename Class, Ret(Class:: *Member)(Args...)>
void connect(Class *instance);

It is then used like this:

class A
{
  void f(int)
  { }

  void foo()
  {
    Delegate<void(int)> d;
    d.connect<A, &A::f>(this);
  }
};

I would like to create a proxy template function MyConnect that simply forwards the arguments to Delegate's connect

class A
{
  void f(int)
  { }

  void foo()
  {
    MyConnect<A, &A::f>(this);
  }

  template <???>
  void MyConnect(???)
  {
    Delegate<void(int)> d;
    d.connect<???, ???>(this);
  } 
};

I cannot do this template <typename Class, void(Class::*Member)(int)> because I do not know what the return type is nor do I know the number of function arguments

With C++17, you might use auto :

template <typename C, auto m>
void MyConnect()
{
    Delegate<void(int)> d;
    d.connect<C, m>(this);
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM