lundi 26 juin 2017

Overloading ->* to point to member object functions

I have a class from another library that I'm wrapping and cannot modify, say:

class A {
public:
  float mem(int a);
  /* many more functions */
};

I'm wrapping this with a class that will hold an instance of A as a member variable:

class B {
public:
  A get() { return m_a; }
private:
  A m_a;
};

Now, to call member functions of A, I could definitely do:

B.get().mem(2);

This can be cumbersome with repeated calls to member functions, and writing wrappers for all of the member functions isn't really viable. So my thought was to overload the ->* operator to call these functions:

B->*mem(2);

What I'm struggling with is the type of the function that needs to be passed to the ->* operator. I'm actually having trouble finding any examples of anyone actually overloading this operator without completely changing its functionality. My best guess is that it would resemble something like this:

template<class A::func, class return_type, class... args>
return_type operator->*(A::func(args)) {
  return m_a.*(A::func(args...));
}

I would figure it has to be templated, but can template deduction correctly pick out the function, its arguments, and its return type? Or maybe a decltype(auto) for the return type? What is the actual type of a member function?

Aucun commentaire:

Enregistrer un commentaire