I found that lvalue
lambda closures can always be passed as rvalue
function parameters.
See the following simple demonstration.
#include <iostream>
#include <functional>
using namespace std;
void foo(std::function<void()>&& t)
{
}
int main()
{
// Case 1: passing a `lvalue` closure
auto fn1 = []{};
foo(fn1); // works
// Case 2: passing a `lvalue` function object
std::function<void()> fn2 = []{};
foo(fn2); // compile error
return 0;
}
Case 2 is the standard behavior (I just used a std::function
for demonstration purposes, but any other type would behave the same).
How and why does case 1 work ? What is the state of fn1
closure after the function returned ?