I would like to create a simple no-op std::function
object with an arbitrary signature. To that end, I've created two functions:
template <typename RESULT, typename... ArgsProto>
std::function<RESULT(ArgsProto...)> GetFuncNoOp()
{
// The "default-initialize-and-return" lambda
return [](ArgsProto...)->RESULT { return {}; };
}
template <typename... ArgsProto>
std::function<void(ArgsProto...)> GetFuncNoOp()
{
// The "do-nothing" lambda
return [](ArgsProto...)->void {};
}
Each of these works well enough (obviously the first version might create uninitialized data members in the RESULT
object, but in practice I don't think that would be much of a problem). But the second, void
-returning version is necessary because return {};
never returns void
(this would be a compile error), and it can't be written as a template-specialization of the first because the initial signature is variadic.
So I am forced to either pick between these two functions and only implement one, or give them different names. But all I really want is to easily initialize std::function
objects in such a way that, when called, they do nothing rather than throwing an exception. Is this possible?
Note that the default constructor of std::function
does not do what I want.