以下代码编译没有问题:
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
associate
是dimse_pm
的成员函数,不会超载。调用inject函数的对象ul
存储std::function<void(scx*, std::unique_ptr<property>)>
。我想摆脱这个指针。 clang 3.5和gcc 4.7.3都会出现此错误。
答案 0 :(得分:3)
[=](upperlayer::scx* sc, std::unique_ptr<upperlayer::property> rq) {
associate(sc, std::move(rq));
}
这个lambda函数对象使用两个参数(associate
和upperlayer::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
。