As a small exercise in functional programming in C++, I want to overload the operator*
to allow me to compose 2 functions.
What stumps me is that if I define my operator like this:
std::function<float(float)> operator*(std::function<float(float)> func1, std::function<float(float)> func2)
{
// ...
}
I cannot use it with regular functions as in:
float f(float);
float g(float);
auto composite = f*g;
Because I get:
error: invalid operands of types ‘float(float)’ and ‘float(float)’ to binary ‘operator*’
If I try to add a function pointer only version as in:
std::function<float(float)> operator*(float(*func1)(float) , float(*func2)(float))
{
return [func1, func2](float x) { return func1(func2(x)); };
}
I get this error:
error: ‘std::function<float(float)> operator*(float (*)(float), float (*)(float))’ must have an argument of class or enumerated type
I was able to get it to work by adding a make_function
helper, but that really makes the code ugly.
Is there a way to get this operator to automatically cast functions to std::function
instances?