如何使用boost :: bind将成员函数绑定到任何对象

时间:2015-11-12 07:41:16

标签: c++ boost member-function-pointers boost-bind boost-function

我试图使用boost :: bind boost :: function来实现某些功能,但却无法使其正常工作。

我看到一个对象的how to bind a member function,其中有一些参数稍后将用作void / void函数。但是如何将它绑定到任何对象。

我希望有一个指向我的类的成员函数的指针(可以与该类的任何对象一起使用)和带有绑定的参数:

#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" ) );
}

我试过了:

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 );

但它无法编译:

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

注意:我还没有使用C ++ 11,所以请不要auto ...; - )

1 个答案:

答案 0 :(得分:4)

许多微妙的调整,大多数简化:

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"));
}

注释

  • 使用boost::function<void(A const&)>,因为您取消引用 const 迭代器
  • 使用A::dispatch(vect, boost::bind(&A::func1, _1, 3));
  • 标记func1func2 const

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

输出:

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)
相关问题