std :: bind在libstdc ++中产生编译错误

时间:2014-09-21 14:51:32

标签: c++ c++11 libstdc++

以下代码编译没有问题:

void dimse_pm::f()
{
       ul.inject(upperlayer::TYPE::A_ASSOCIATE_RQ,
           [=](upperlayer::scx* sc, std::unique_ptr<upperlayer::property> rq) { 
               associate(sc, std::move(rq)); 
            } 
       );
}

但是,使用std :: bind的以下代码无法编译。

void dimse_pm::f()
{
    using namespace std::placeholders;
    ul.inject(upperlayer::TYPE::A_ASSOCIATE_RQ,
         std::bind(&dimse_pm::associate, this, _1));
}

这是我得到的错误:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:1925:2: error: no matching function for call to     object of type 'std::_Bind<std::_Mem_fn<void (dimse_pm::*)(upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >)> (dimse_pm *, std::_Placeholder<1>)>'
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:2298:33: note: in instantiation of member function  'std::_Function_handler<void (upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >), std::_Bind<std::_Mem_fn<void (dimse_pm::*)(upperlayer::scx *,  std::unique_ptr<upperlayer::property, std::default_delete<upperlayer::property> >)>
  (dimse_pm *, std::_Placeholder<1>)> >::_M_invoke' requested here
        _M_invoker = &_My_handler::_M_invoke;
                                   ^
dimse_pm.cpp:25:14: note: in instantiation of function template specialization 'std::function<void (upperlayer::scx *, std::unique_ptr<upperlayer::property, std::default_delete<upperlayer::property>
  >)>::function<std::_Bind<std::_Mem_fn<void (dimse_pm::*)(upperlayer::scx *, std::unique_ptr<upperlayer::property, std::default_delete<upperlayer::property> >)> (dimse_pm *, std::_Placeholder<1>)> >' requested here
         std::bind(&dimse_pm::associate, this, _1));
         ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:1211:2: note: candidate template ignored: substitution failure [with _Args = <upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >>]: no matching function for call to object of type 'std::_Mem_fn<void (dimse_pm::*)(upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >)>'
    operator()(_Args&&... __args)
    ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:1225:2: note: candidate template ignored: substitution failure [with _Args = <upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >>]: no matching function for call to object of type 'const std::_Mem_fn<void (dimse_pm::*)(upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >)>'
    operator()(_Args&&... __args) const
    ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:1239:2: note: candidate template ignored: substitution failure [with _Args = <upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >>]: no matching function for call to object of type 'volatile std::_Mem_fn<void (dimse_pm::*)(upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >)>'
    operator()(_Args&&... __args) volatile
    ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/functional:1253:2: note: candidate template ignored: substitution failure [with _Args = <upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >>]: no matching function for call to object of type 'const volatile std::_Mem_fn<void (dimse_pm::*)(upperlayer::scx *, std::unique_ptr<upperlayer::property,
  std::default_delete<upperlayer::property> >)>'
    operator()(_Args&&... __args) const volatile

associatedimse_pm的成员函数,不会超载。调用inject函数的对象ul存储std::function<void(scx*, std::unique_ptr<property>)>。我想摆脱这个指针。 clang 3.5和gcc 4.7.3都会出现此错误。

1 个答案:

答案 0 :(得分:3)

[=](upperlayer::scx* sc, std::unique_ptr<upperlayer::property> rq) { 
     associate(sc, std::move(rq)); 
}

这个lambda函数对象使用两个参数(associateupperlayer::scx*)加上std::unique_ptr<upperlayer::property>&&指针调用this

std::bind(&dimse_pm::associate, this, _1)

此绑定表达式对象使用一个参数加上associate指针调用this,该指针失败,因为associate需要两个参数加上this指针。

你想要的是这个:

std::bind(&dimse_pm::associate, this, _1, _2)

this, _1, _2表示this将被捕获并作为隐式this参数转发给associate,绑定表达式的调用运算符的第一个参数将被转发为associate的第一个显式参数和绑定表达式的调用运算符的第二个参数将作为第二个显式参数转发给associate