So, I know that you can pass a function as an argument like so:
int a(int x) {
return x + 1;
}
int b(int (*f)(int), int x) {
return f(x); // returns x + 1
}
I also know you can have a function with default arguments, like so:
int a(int x = 1) {
return x;
}
a(2); // 2
a(); // 1
However, how do I pass a function with default arguments to a function and preserve this behavior?
I've tried the following:
int b(int (*f)(int), int x) {
f(x); // works as expected
f(); // doesn't work because there aren't enough arguments to f
}
and
int b(int (*f)()) {
f(); // doesn't work because it cannot convert int (*)(int) to int (*)()
}
int a(int x = 1), will turn youra();intoa(1);and compile that.Ccan do. With a callable object, you basically can do anything, including moving the entire parameter determination away from the call invocation site, all due tooperator(). This is something you can't do with "dumb" function pointers.void f(int=0)andvoid g(int=1). You will have to wrap the function together with the default parameter.