Why can't we call a function from a function with a default argument?

Program:

#include <iostream>

void foo(void (*bar)()){ bar(); };

void foo(int a = 5)
{
    std::cout << a << std::endl;
}

int main()
{ 
    foo(foo); //Error
}

Demo

I expected to eventually be called foo(5). In contrast, the following program works fine:

#include <iostream>

void foo(void (*bar)()){ bar(); };

void foo()
{
    std::cout << 5 << std::endl;
}

int main()
{ 
    foo(foo); //OK
}

Demo

Could you explain this difference?

+4
source share
1 answer

In the first example, although foo has a default argument, its type is void (bar *) (int). Having a default argument allows you to call foo without specifying the value of the argument explicitly, but there is still an int argument. It's just that its value is automatically populated (at compile time).

+2
source

All Articles