Search code examples
c++boostpointer-to-memberboost-bindboost-function

How to use boost::bind to bind a member-function to ANY object


I'm trying to achieve something with boost::bind boost::function but can't make it work.

I see how to bind a member function of an object with some arguments to be used later as a void/void function. But how to bind it to any object.

I'd like to have a pointer to a member function of my class (that could be used with any object of the class) with binded arguments:

#include "boost/function.hpp" 
#include "boost/bind.hpp" 

class A
{
public:
    A() {}

    void func1( int i );
    void func2( const std::string& s );

    static void dispatch( std::vector<A>& vect, /* boost::function parameter "func" */ ) // what parameter should I set?
    {
        for ( std::vector<A>::iterator iter = vect.begin();
              iter != vect.end();
              ++iter )
        {
            // what syntax should I use?
            //(*iter).(*func)();
        }
    }
};

int main()
{
    std::vector<A> vect;

    // have func1(3) be called for all objects contained in vect
    // this does not compile because boost expects an object to bind the function to
    A::dispatch( vect, boost::bind( boost::mem_fn(&A::func1), 3 ) );
    // have func2("hello") be called for all objects contained in vect
    // this does not compile because boost expects an object to bind the function to
    A::dispatch( vect, boost::bind( boost::mem_fn(&A::func2), "hello" ) );
}

I tried that:

static void dispatch( const std::vector<A>& vect, boost::_mfi::mf0<void, A> func )
...
boost::_mfi::mf1<void,A,int> func( boost::mem_fn(&A::func1) );
boost::_mfi::mf0<void,A> binded( boost::bind( func, 3 ) );
A::dispatch( vect, binded );

But it fails to compile:

Erreur  1   error C2664: 'boost::_mfi::mf0<R,T>::mf0(void (__thiscall A::* )(void))' : impossible de convertir le paramètre 1 de 'boost::_bi::bind_t<R,F,L>' en 'void (__thiscall A::* )(void)' b:\dev\vobs_bci\public\lib\btle\src\btle\notifierdispatcher.cpp 99

Note: I'm unfortunatley not using C++11 yet, so please no auto...;-)


Solution

  • Many subtle tweaks, most simplifications:

    Live On Coliru (c++03)

    #include "boost/function.hpp" 
    #include "boost/bind.hpp" 
    #include <vector>
    #include <iostream>
    
    class A
    {
    public:
        A() {}
    
        void func1(int i) const                { std::cout << __PRETTY_FUNCTION__ << "(" << i << ")\n"; } 
        void func2(const std::string& s) const { std::cout << __PRETTY_FUNCTION__ << "(" << s << ")\n"; } 
    
        static void dispatch(const std::vector<A>& vect, boost::function<void(A const&)> const& func)
        {
            for ( std::vector<A>::const_iterator iter = vect.begin();
                  iter != vect.end();
                  ++iter )
            {
                func(*iter);
            }
        }
    };
    
    int main()
    {
        std::vector<A> vect(3);
    
        A::dispatch(vect, boost::bind(&A::func1, _1, 3));
        A::dispatch(vect, boost::bind(&A::func2, _1, "hello"));
    }
    

    Notes

    • Use boost::function<void(A const&)> because you dereference const iterators
    • Use A::dispatch(vect, boost::bind(&A::func1, _1, 3));
    • Mark func1 and func2 const

      void func1(int i) const;
      void func2(std::string const& i) const;
      

    Output:

    void a::func1(int) const(3)
    void a::func1(int) const(3)
    void a::func1(int) const(3)
    void a::func2(const std::string &) const(hello)
    void a::func2(const std::string &) const(hello)
    void a::func2(const std::string &) const(hello)