c++ - std::bind(): bind lambda with rvalue reference as argument -
i playing std::bind
, rvalue references, still don't figure out how works, have next code:
class dog { public: dog(const string &name) : name_(name) { cout << "dog::ctor" << endl; } string getname() { homecoming name_; } private: string name_; }; auto bind_fun = bind([](dog &&d){ cout << d.getname() << endl; }, dog("dogabc")); bind_fun();
when commenting out bind_fun()
, or if lambda takes dog&
rather dog&&
, code run fine expected output. when bind_fun()
left uncommented, next compile time error:
test3.cpp:109:3: error: no matching function phone call object of type 'std::__1::__bind<<lambda @ test3.cpp:108:17>, dog>' f(); ^ /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1749:9: note: candidate template ignored: substitution failure [with _args = <>]: implicit instantiation of undefined template 'std::__1::__bind_return<<lambda @ test3.cpp:108:17>, std::__1::tuple<dog>, std::__1::tuple<>, false>' operator()(_args&& ...__args) ^ /applications/xcode.app/contents/developer/toolchains/xcodedefault.xctoolchain/usr/bin/../lib/c++/v1/functional:1758:9: note: candidate template ignored: substitution failure [with _args = <>]: implicit instantiation of undefined template 'std::__1::__bind_return<const <lambda @ test3.cpp:108:17>, const std::__1::tuple<dog>, std::__1::tuple<>, false>' operator()(_args&& ...__args) const ^ 1 error generated.
my questions are:
whybind_fun()
can not called(won't compile) when lambda takes rvalue reference? what difference between using reference , rvalue reference arguments lambda here?
the specification std::bind
rather dense. in brief, plain bound argument (not bind expression, not reference_wrapper
, , not placeholder) passed bound function std::forward<vi>(tid)
vi
tid cv &
, cv
cv-qualifiers of phone call wrapper, tid
type decay_t<ti>
, ti
type passed bind
, , tid
"an lvalue of type tid
constructed std::forward<ti>(ti)
", , ti
argument passed bind
.
applying call, see ti
dog
, ti
dog("dogabc")
. tid
dog
, , vi
cv dog &
, means std::forward<vi>(tid)
lvalue, , compiler complains because lambda takes rvalue reference parameter, , rvalue reference parameter cannot bind lvalue.
c++ c++11 lambda stdbind
No comments:
Post a Comment