c++ - C++11 lambda can be assigned to std::function with incorrect signature -
the following compiles , runs (under apple llvm version 6.1.0 , visual c++ 2015):
#include <functional> #include <iostream> struct s { int x; }; int main(int argc, char **argv) { std::function<void (s &&)> f = [](const s &p) { std::cout << p.x; }; f(s {1}); return 0; } why doesn't assignment std::function<void (s &&)> f = [](const s &p) { std::cout << p.x; }; generate error? function accepting rvalue reference should not have same signature function accepting const lvalue reference, should it? dropping const lambda's declaration generate error expected.
to expand on existing comment , answer:
the point of std::function<r(a...)> can wrap function or functor can called a... , have result stored in r.
so, example,
std::function<int(int)> f = [](long l) { return l; }; is peachy.
so have ask when see this: if have lambda taking const t &, , have expression of type t && (or, more accurately, have xvalue of type t), can use expression call lambda?
yes, can.
and if can, std::function supposed able store functor. that's pretty main point of std::function.
Comments
Post a Comment