This is why function pointers are weak. You have two good answers why this does not work - the function template does not know that your function has a default argument.
The solution is to not pass a function pointer - pass a function object. In this case, you can rewrite lambda:
eval([](int i){ return sum(i); });
which seems like a really annoying true way to basically just write sum in a way that really works. But such things happen periodically (what if sum was an overloaded name?), So itβs convenient to have only the lambdafying macro (requires C ++ 14):
#define AS_LAMBDA(func) [&](auto&&... args) -> decltype(auto) { \ return func(std::forward<decltype(args)>(args)...); }
so you can write:
eval(AS_LAMBDA(sum));
This will work with default arguments, overloaded names, and even work to bind member functions:
struct X { int i; int add(int j) { return i + j; } }; X x{42}; eval(AS_LAMBDA(x.add));
Barry source share