Why there is no compilation error at (@)
in below code? I thought lamb
is an lvalue so it will not get bound to an rvalue reference.
using FunctionType = std::function<void()>;
using IntType = int;
struct Foo {
void bar(FunctionType&&) {}
void baz(IntType&&) {}
};
Foo foo;
foo.bar([]() {}); //OK
auto lamb = []() {};
foo.bar(lamb); //(@) No compilation error?!
foo.baz(5); //OK
int i = 5;
foo.baz(i); //Error
Since
lamb
is a lambda and not astd::function
, a temporarystd::function
has to be created and passed tobar()
. The temporary binds to the rvalue reference.Your code is equivalent to this:
You would get a compile error if you did this instead: