Can someone help me to understand why the following code causes an error?
class A
{
public:
float& operator()()
{
return _f;
}
private:
float _f = 1;
} a;
auto& foo()
{
std::function<float()> func = a;
return func();
}
int main()
{
std::cout << foo() << std::endl;
}
Error:
error: non-const lvalue reference to type 'float' cannot bind to a temporary of type 'float'
return func();
^~~~~~
1 error generated.
Here, in operator()
, I return a reference to _f
and consequently, I thought func()
is not a temporary.
It would be great if someone helps me understand.