I have started learning default arguments and came across code like
void func(int, int = 4); //second parameter has default argument
func(5); //default argument will be used here as expected
As we can see when we call func
in the above function call, we can omit the second argument and the compiler will still call the correct function as if we explicitly passed 4
as the second argument.
But when we call a function through a function pointer(as shown below), the default argument cannot be omitted. My question is why is there this restriction? I want to know the reason behind why the C++ standard does not allow to omit the second argument when we call the function using a function pointer.
void func(int, int = 4);
void (*ptr)(int, int) = func;
int main()
{
ptr(5); //why does c++ disallow omitting the second argument here
}
I think the reason is most probably related to how a pointer can be used but I can't figure out by myself the exact reason as I'm a C++ beginner. Note also that I'm not looking for a solution to the above shown code but instead want to know the reason of why when calling a function through a function pointer, default arguments cannot be omitted.
Note
Note that I already know that C++ standard does not allow omitting the second argument when calling through pointer like ptr(5)
. I want to know the reasoning behind this restriction.
I also tried searching the web before asking this question but did not find similar question.