C++11: perfect forward through base class pointer to derived class method

501 Views Asked by At

I am looking to perfectly forward a parameter pack through a base class pointer to a derived class that holds a method pointer of a particular type, and then call that method pointer with the perfectly forwarded parameter pack. The Sender<cARGS> class has the container std::set<ReceiverBase<cARGS>*>. I want to iterate the container with the same parameter pack values (this I know how to do). Different ReceiverBase<cARGS>* will have different derived class (Receiver<cPARENT, cARGS>) method pointer types, but the parameter pack types will be the same (the cPARENT type in the derived class is the method pointer type). I have this working as expected with parameter pack being copied. Now I wish to add perfect forwarding like so:

template<typename ...Ts>
void Notify(Ts&&...args)
{
    (m_parent->*m_notifyMethod)(std::forward(args)...);
}

I understand that I cannot have a virtual template method. I tried to use employ a double dispatch visitor pattern, but I'm not sure how to do this when I cannot know the type of the class that hold the method pointer.

template <typename ...cARGS>
class RecevierBase
{
public:
    virtual void Notify(cARGS...) = 0;
};


template <typename cPARENT, typename ...cARGS>
class Recevier : public RecevierBase<cARGS...>
{
    typedef void (cPARENT::* NOTIFY_METHOD)(cARGS...);
public:
    Recevier(cPARENT *parent, NOTIFY_METHOD notifyMethod) :
        m_parent(parent), m_notifyMethod(notifyMethod)
    {  }

    virtual void Notify(cARGS... args)
    {
        (m_parent->*m_notifyMethod)(args...);
    }

private:
    cPARENT *m_parent;
    NOTIFY_METHOD m_notifyMethod;
};
0

There are 0 best solutions below