Reputation: 313
Consider the following:
int foo(int x , int z = 0);
int foo(int x, int y , int z = 0);
If I call this function like so:
foo( 1 , 2);
How does the compiler know which one to use?
Upvotes: 5
Views: 152
Reputation: 11787
Compiler will report Ambiguous function overload. As you cannot figure out which function will b called so does the compiler
Upvotes: 2
Reputation: 7946
That is a nice question. But it will not compile because of ambigious call to foo()
. You can remove this ambiguity by using different datatypes in function signature.
For more detail about default parameter and function overloading see http://www.smart2help.com/e-books/ticpp-2nd-ed-vol-one/Chapter07.html
Upvotes: 4
Reputation: 206518
It won't and hence this example will not compile cleanly, it will give you an compilation error.
It will give you an ambiguous function call error.
int foo(int x , int z = 0){return 0;}
int foo(int x, int y , int z = 0){return 10;}
int main()
{
foo( 1 , 2);
return 0;
}
Output:
prog.cpp: In function ‘int main()’:
prog.cpp:6: error: call of overloaded ‘foo(int, int)’ is ambiguous
prog.cpp:1: note: candidates are: int foo(int, int)
prog.cpp:2: note: int foo(int, int, int)
Upvotes: 8