I want to return a lambda object from a function without casting it to a function pointer or function object. More specifically, I want to leave it to the client to decide whether or not to cast to a function object or retain the lambda as an anonymous function:
template<typename F> // F may be a std::function, boost::function, or lambda object
auto func(F f) -> decltype(???) // what do I put in here?
{
return [=]()
{
return f(someParameter);
}
}
This code doesn't work because I don't know what type to deduce. One option might be to copy the body of the lambda into the decltype
call. Surely there is a better way!
My reasoning for wanting to do it this way is that I want to leave open the possibility of the compiler more intelligently inlining or unwrapping the lambdas. Does this reasoning make sense? If so, why, and when is it better simply to return std::function
? Could this method ever be slower at compile time?