0

I try to give a reference to a member function (func) to a function (Multiprotocol) and call it with an object (z) of this class. But i have problems with the line

result = z.*f(std::forward<Args>(args)...));

I tried things like

z.(*f) (std::forward<Args>(args)...)) 

and

z.(*(&f)(std::forward<Args>(args)...));

But i don't knwow how to this. Can anybody help me?

class test
{ 
   public:
        static int func()
        {
            std::cout << "in func";
        }
 };


class MultiProtocol
{
   public:

      template<typename Function, typename... Args>
      bool functionImpl(Function f, Args&&... args)
      {
         int result = 0;
         result = z.*f(std::forward<Args>(args)...));
         return result;
      }

      private:
      test z;
};

Main is:

int main()
{
MultiProtocol rr;
rr.functionImpl(&test::func);

}

1 Answers1

0

Nearly, it is:

(z.*f)(std::forward<Args>(args)...)

Cannot be

  • z.(*f) (std::forward<Args>(args)...))
  • z.(*(&f)(std::forward<Args>(args)...));

as we should use operator .* (or ->*).

z.*f(std::forward<Args>(args)...) would be valid if f(args...) would return int test::* (pointer on member) as it is actually z .* (f(std::forward<Args>(args)...)).

Jarod42
  • 203,559
  • 14
  • 181
  • 302