【问题标题】:How to use boost::bind to bind a member-function to ANY object如何使用 boost::bind 将成员函数绑定到任何对象
【发布时间】:2016-02-13 11:33:11
【问题描述】:

我正在尝试使用 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...;-)

【问题讨论】:

  • 在得到答案后编辑问题(以删除 const)并不是很优雅。当然,把const从我的回答中放到各个地方,然后
  • @sehe:对不起,我只是在评论你的回答。尝试删除代码中的所有const,但无法编译,报告impossible de convertir le paramètre 2 de 'boost::_bi::bind_t&lt;R,F,L&gt;' en 'boost::function&lt;Signature&gt; &amp;'

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


【解决方案1】:

许多细微的调整,大部分的简化:

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&lt;void(A const&amp;)&gt;,因为您取消引用const 迭代器
  • 使用A::dispatch(vect, boost::bind(&amp;A::func1, _1, 3));
  • 标记func1 和func2 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)

【讨论】:

  • 谢谢sehe,当函数为const 时,这工作得很好,但是当它们不是const 时我无法使其工作(在我的OP 中就是这种情况,除非我通过了当我为 OP 创建我的 MCVE 时,这是一个错误的向量的 const ref)。真的很抱歉。
  • 字面上只是删除了const:coliru.stacked-crooked.com/a/a335b444d44d6a08(好吧,const_ 在iterator 的情况下)
  • 知道了。我实际上在包含其他问题时遇到了其他问题(有趣的是必须定义在 const 引用中传递给函数的对象(没有前向声明),否则 boost::bind 拒绝绑定。它现在可以工作了。非常感谢您的帮助,很抱歉const ;-)
  • 好吧,你拿一个成员的地址。编译器需要知道类的样子是有道理的
  • 我的意思是函数参数(比如std::string for func2)。看到我刚刚问的这个问题:stackoverflow.com/questions/33667356/…
猜你喜欢
  • 1970-01-01
  • 2011-06-23
  • 2019-09-14
  • 2011-12-07
  • 1970-01-01
  • 2011-01-19
  • 2013-11-12
  • 2013-05-12
  • 1970-01-01
相关资源
最近更新 更多