I'm trying to call an overloaded function which operates on function pointers that have parameters with default values.
void originalFunction1 (int a = 0) {printf("I'm #1 and a is %d",a);}
void originalFunction2 () {printf("I'm #2");}
void overloadedFunction (void (*fptr)(void))
{
fptr();
}
void overloadedFunction (void (*fptr)(int))
{
overloadedFunction( (void(*)(void)) fptr);
}
int main()
{
overloadedFunction(originalFunction1);
overloadedFunction(originalFunction2);
// output is:
// I'm #1 an a is -1073743272
// I'm #2
}
As the answers to this question points out, default values are not part of the function signature and also can't be repeated during the (function pointer -) parameter definition. As my example shows, they can be cast away for calling, but they will then not be initialized to their default value.
Is there any way to work around this?
I can't modify the original function but I know the default value. I can modify both the main overloaded function as well as the redirects. The fptr's will always only be called without parameters. In reality, there are more overloaded functions as also the return type differs, but I can cast that away more easily.