为什么使用`:: boost :: bind`的代码会出现编译错误?

Omn*_*ous 2 c++ boost compiler-errors

这段代码:

#include <boost/signals.hpp>
#include <boost/bind.hpp>
#include <boost/mem_fn.hpp>
#include <iostream>

class Recorder : public ::boost::signals::trackable {
 public:
   void signalled() {
      const void *me = this;
      ::std::cerr << "Recorder at " << me << " signalled!\n";
   }
};

void signalled()
{
   ::std::cerr << "Signalled!\n";
}

int main(int argc, const char *argv[])
{
   ::boost::signal<void ()> sig;
   sig.connect(&signalled);
   {
      Recorder r;
      sig.connect(::boost::bind(&Recorder::signalled, &r, _1));
      sig();
   }
   sig();
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

正在生成这些编译器错误:

In file included from move_constructor.cpp:2:
/usr/include/boost/bind.hpp: In instantiation of ‘boost::_bi::result_traits<boost::_bi::unspecified, void (Recorder::*)()>’:
/usr/include/boost/bind/bind_template.hpp:15:   instantiated from ‘boost::_bi::bind_t<boost::_bi::unspecified, void (Recorder::*)(), boost::_bi::list2<boost::_bi::value<Recorder*>, boost::arg<1> > >’
move_constructor.cpp:25:   instantiated from here
/usr/include/boost/bind.hpp:67: error: ‘void (Recorder::*)()’ is not a class, struct, or union type
In file included from /usr/include/boost/function/detail/maybe_include.hpp:13,
                 from /usr/include/boost/function/function0.hpp:11,
                 from /usr/include/boost/signals/signal_template.hpp:38,
                 from /usr/include/boost/signals/signal0.hpp:24,
                 from /usr/include/boost/signal.hpp:19,
                 from /usr/include/boost/signals.hpp:9,
                 from move_constructor.cpp:1:
/usr/include/boost/function/function_template.hpp: In static member function ‘static void boost::detail::function::void_function_obj_invoker0<FunctionObj, R>::invoke(boost::detail::function::function_buffer&) [with FunctionObj = boost::_bi::bind_t<boost::_bi::unspecified, void (Recorder::*)(), boost::_bi::list2<boost::_bi::value<Recorder*>, boost::arg<1> > >, R = void]’:
/usr/include/boost/function/function_template.hpp:904:   instantiated from ‘void boost::function0<R>::assign_to(Functor) [with Functor = boost::_bi::bind_t<boost::_bi::unspecified, void (Recorder::*)(), boost::_bi::list2<boost::_bi::value<Recorder*>, boost::arg<1> > >, R = void]’
/usr/include/boost/function/function_template.hpp:720:   instantiated from ‘boost::function0<R>::function0(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = boost::_bi::bind_t<boost::_bi::unspecified, void (Recorder::*)(), boost::_bi::list2<boost::_bi::value<Recorder*>, boost::arg<1> > >, R = void]’
/usr/include/boost/function/function_template.hpp:1040:   instantiated from ‘boost::function<R()>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not::value, int>::type) [with Functor = boost::_bi::bind_t<boost::_bi::unspecified, void (Recorder::*)(), boost::_bi::list2<boost::_bi::value<Recorder*>, boost::arg<1> > >, R = void]’
/usr/include/boost/signals/slot.hpp:111:   instantiated from ‘boost::slot<SlotFunction>::slot(const F&) [with F = boost::_bi::bind_t<boost::_bi::unspecified, void (Recorder::*)(), boost::_bi::list2<boost::_bi::value<Recorder*>, boost::arg<1> > >, SlotFunction = boost::function<void()>]’
move_constructor.cpp:25:   instantiated from here
/usr/include/boost/function/function_template.hpp:152: error: no match for call to ‘(boost::_bi::bind_t<boost::_bi::unspecified, void (Recorder::*)(), boost::_bi::list2<boost::_bi::value<Recorder*>, boost::arg<1> > >) ()’
Run Code Online (Sandbox Code Playgroud)

这是在安装了Fedora 11 boost-1.37.0软件包的Fedora 11机箱上的g ++ 4.4.1.

这段代码对我来说似乎完全是犹太人.我不明白这里发生了什么,模板扩展相关错误的迷宫非常混乱.有谁知道问题是什么?

Kor*_*icz 6

sig.connect(::boost::bind(&Recorder::signalled, &r, _1));
Run Code Online (Sandbox Code Playgroud)

什么是_1离开这里占位?它是不需要的,连接期望void -> void功能.如果您将删除不必要的占位符,代码将编译.

您提供&Recorder::signalled- 类型的成员函数void Recorder::(void),正确绑定记录器指针,将其更改为void -> void,然后另外留下占位符_1- 显然是错误的.