13

我只是写了一个简单的例子来测试 boost::bind。我用它来实例化一个模板成员函数,但它不能用 g++ 4.6.0 编译。我不知道有什么问题。这是代码:

#include <boost/bind.hpp>

struct Functor
{
  void operator()()
  {

  }
};

struct DerivedFinishAction
{
  DerivedFinishAction()
  {}

  void Inc()
  {

  }

  template <typename T>
  void TmplFunc(T t)
  {
    (boost::bind(&DerivedFinishAction::BindFunc<T>, this , t))();
  }

  template <typename T>
  void BindFunc(T t)
  {
    t();
  }

  void Func()
  {
    Functor f;
    TmplFunc(f); // this is OK
    TmplFunc(boost::bind(&DerivedFinishAction::Inc, this)); // compile error
  }
};

int main(int argc, char *argv[])
{

  return 0;
}

并且 g++ 给出以下错误:

In file included from /usr/include/boost/bind.hpp:22:0,
                 from testBind.cpp:1:
/usr/include/boost/bind/bind.hpp: In member function ‘void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = boost::_mfi::mf1<void, DerivedFinishAction, boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > > >, A = boost::_bi::list0, A1 = boost::_bi::value<DerivedFinishAction*>, A2 = boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > >]’:
/usr/include/boost/bind/bind_template.hpp:20:59:   instantiated from ‘boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()() [with R = void, F = boost::_mfi::mf1<void, DerivedFinishAction, boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > > >, L = boost::_bi::list2<boost::_bi::value<DerivedFinishAction*>, boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > > >, boost::_bi::bind_t<R, F, L>::result_type = void]’
testBind.cpp:24:5:   instantiated from ‘void DerivedFinishAction::TmplFunc(T) [with T = boost::_bi::bind_t<void, boost::_mfi::mf0<void, DerivedFinishAction>, boost::_bi::list1<boost::_bi::value<DerivedFinishAction*> > >]’
testBind.cpp:37:58:   instantiated from here
/usr/include/boost/bind/bind.hpp:313:9: error: invalid use of void expression

有人可以帮忙解释一下吗?为什么第一个实例化没问题,而第二个实例导致编译错误?

4

1 回答 1

14

这里有一个(非显而易见的)特征boost::bindhttp://www.boost.org/libs/bind/#nested_binds

如果你写:

void func1(int len) {return len+1;};
int func2(std::string str) {return str.length();};

assert(
    boost::bind(func1, boost::bind(func2, _1) )("Hello")
    == 6 );

boost::bind假设您的意思是“运行func2"Hello"然后运行func1结果”。这允许更有趣的部分函数应用。

在您的程序中,您有一个表达式,相当于:

boost::bind(&DerivedFinishAction::BindFunc<...>, 
            this, 
            boost::bind(&DerivedFinishAction::Inc, this))

所以boost::bind试图弄清楚如何DerivedFinishAction::Inc在它的参数上运行,所以它可以将结果传递给DerivedFinishAction::BindFunc<...>. 但DerivedFinishAction::Inc返回 void,不能传入DerivedFinishAction::BindFunc<...>. 因此,您会收到编译器错误:

/usr/include/boost/bind/bind.hpp:313:9: error: invalid use of void expression

编辑:根据文档,您可以使用它protect来实现您想要的行为:

#include <boost/bind/protect.hpp>
...
TmplFunc(boost::protect(boost::bind(&DerivedFinishedAction::Inc, this))); // no longer an error
...
于 2012-05-15T02:39:50.033 回答