c++boostboost-bindboost-function

How to create boost::function from template signature


Recently I was trying to create flexible observer pattern implementation which hides boost::signal. I almost succeeded.

I have a Observer class which has to have an update method matching signature provided by template parameter.

Example of use:

Observable<void(float, float)> observable;
Observer<void(float, float)> observer;
observable.attach(&observer);
observable.notify(Observable::Arguments(10.0f, 1.0f)); // invokes observer->update(10.0f, 1.0f);

Everything works just fine if observer does not have overloaded update method. In that case boost::bind can not deduce correct method to use. Unfortunately I can't use explicit casting because I don't know update arguments (this information is in FunctionSignature).

Following method causes troubles:

class Observable <typename FunctionSignature>
{
...
template <class DerivedObserverClass>
void attach(DerivedObserverClass* observer)
{
    STATIC_ASSERT((boost::is_base_of<ObserverType, DerivedObserverClass>::value));

    ConnectionsMap::iterator it = connections.find(observer);
    if (it == connections.end() || !it->second.connected()) {
        // i would like to do something like 
            // boost::function<FunctionSignature> f;
        // f = boost::bind(&static_cast<FunctionSignature>DerivedObserverClass::update, observer, _1);

        // singnalSlot is defined as boost::signal<FunctionSignature>
        // this works as long, as Derived class doesn't have overloaded update method
        connections[observer] = signalSlot.connect(boost::bind(&DerivedClass::update, observer, _1));
    } else {
        throw std::invalid_argument("Observer already attached.");
    }
}

I think that boost::function could help to solve this problem. I don't know how to bind it with correct member method using only template signature.

Is it even possible?


Solution

  • No, boost::function won't help you either. 13.4.3 says

    Nonstatic member functions match targets of type “pointer-to-member-function;” the function type of the pointer to member is used to select the member function from the set of overloaded member functions.

    This means you cannot take an address of overloaded member function, pass it to any kind of function object (templated or not, boost or std or whatever), and hope the overloading will resolve itself. You need a genuine honest pointer-to-member-function type at the left-hand side of the assignment.

    You will have to convert your FunctionSignature to a pointer-to-member-function type somehow. Here's some old-fashioned template magic that does what you need, for a limited number of function arguments. c++0x might have a better, more general solution.

    template <typename C, typename F>
    struct tomemfun;
    
    template <typename C, typename res>
    struct tomemfun<C, res()>
    {
      typedef res (C::*memfun_t)();
    };
    
    template <typename C, typename res, typename arg1>
    struct tomemfun<C, res(arg1)>
    {
      typedef res (C::*memfun_t)(arg1);
    };
    
    template <typename C, typename res, typename arg1, typename arg2>
    struct tomemfun<C, res(arg1, arg2)>
    {
      typedef res (C::*memfun_t)(arg1, arg2);
    };
    
    // repeat with more arguments as needed
    

    Now you can use

    tomemfun<DerivedClass, FunctionSignature>::memfun_t update = &DerivedClass::update;
    

    and it will resolve to the right overloaded function.

    boost might already have such a conversion template, but I couldn't find it.