I'd prefer letting the library decide what is the default-constructed value for the function<>
instance:
KeyFunc = {}; // uniform initialization (c++11)
// or
KeyFunc = KeyFuncType(); // default construct
Demo with asserts: See it Live on Coliru
#include <functional>
#include <cassert>
int main()
{
using namespace std;
function<int(void)> f = [] { return 42; };
assert(f);
assert(42 == f());
f = nullptr;
assert(!f);
f = {};
assert(!f);
}
If your compiler doesn't have the chops for uniform initialization, use a typedef:
typedef function<int(void)> Func;
Func f = [] { return 42; };
assert(f);
f = Func();
assert(!f);