The following code snippet:
#include <iostream> void does() { std::cout << "do" << std::endl; } void does(bool b = false) { std::cout << "do(bool)" << std::endl; } void fwd(void (*func)(bool)) { func(false); } int main(int, char**) { fwd(&does); fwd(does); fwd(*does); }
clearly causes the following error:
test.cpp:15:10: error: overloaded function with no contextual type information
The compiler cannot determine which of the functions I intend to use. I donβt understand why the code will work correctly when I comment out a line that says:
fwd(*does)
Why can the compiler suddenly solve the ambiguity problem?
int main(int, char**) { fwd(&does); fwd(does); }
In addition, without overloading, the fragment will start correctly with all 3 calls. This snippet works great ...
#include <iostream> void does(bool b = false) { std::cout << "do(bool)" << std::endl; } void fwd(void (*func)(bool)) { func(false); } int main(int, char**) { fwd(&does); fwd(does); fwd(*does); }
I am compiling this with gcc 4.6.3 in a Linux box.
Thanks for the help!
c ++ function function-pointers
Jdr
source share