Deducing type, when using member function pointer as template argument

前端 未结 1 679
闹比i
闹比i 2020-12-18 12:30

When I want to have member function as template argument, is there a way to templetize it without providing Caller type?

struct Foo
{
    temp         


        
相关标签:
1条回答
  • 2020-12-18 12:41

    No, not as you want it. Caller could be deduced if

    1. the pointer to member function were an parameter, not a template parameter. Eg:

      template <class Caller>
      void call(Caller * c, void (Caller::*Func)(int)) { (c->*Func)(6); }
      
    2. it was known beforehand. For example, you could make the call look like this:

      f.arg(this).call<&Bar::printNumber>();
      

      The call function would look similar to this:

      template <class Arg>
      struct Binder
      {
        template<void (Arg::*Func)(int)>
        void operator()() const {
          ...
        }
      };
      

      The arg function would be easy to write (in your case it would return Binder<Bar>, where Bar is deduced from this).

      Not very convenient, IMHO.

    0 讨论(0)
提交回复
热议问题