The function f()
below takes the callable argument fc
whose signature is nicely visible in the function definition.
// scaffolding for function definition
template<typename T>
struct Identity {
using type = T;
};
// still scaffolding for function definition
template<typename T>
using Make_deducible = typename Identity<T>::type;
// function definiton
template <typename T>
T f(T val, Make_deducible<std::function<T(T)>> fc) // we see fc's expected signature
{ return fc(val); }
Note that it can be called in all the following ways:
int g(int i) { return i * i; };
f(5, g); // call with function ptr: rarely used
std::function<int(int)> fo{[](int i){ return i * i; }};
f(6, fo); // call with std::function object: tedious
f(7, [](int i){ return i * i; }); // call with lambda closure: usual use case
What bothers me is that the definition of f()
requires some scaffolding to work. So, my questions is:
Is there a less roundabout way of doing this while keeping the signature of get_val
visible in definition, and still have the function be callable with a lambda?