I have the following code
struct Functor
{
Functor(std::function<void()> task) : _task {task} {}
void operator() () {_task();}
std::function<void()> _task{};
};
Functor run1() //OK
{
return Functor([](){std::cout << "From function" << std::endl;});
}
Functor run2() //Bad. Compile time error
{
return [](){std::cout << "From function" << std::endl;};
}
My questions are:
- Why
run1()is okay butrun2()is not allowed? - Is there a constructor in the struct
Functorthat I can define in order to makerun2()valid as it is? I am asking this because currently the return type ofrun2()in my project isstd::function<void()>and all is good, but I now need to change it into a functor to store some additional properties, but return statement likerun2()are used in many places, and I am reluctant to modify every occurrences of it intorun1().