Can someone explain to me why is foo(5,6) wrong and why the rest are correct? I don't understand. Thanks.
1 2 3 4 5 6 7 8 9 10 11
Given the following overloaded function declarations:
void foo(int a, int b = 10, float c = 0.0f);
void foo(int a, float b = 3.14f);
Check function calls that are not compilable.
foo(5, 6); compilable
foo(8); not compilable
foo(7, 2, 8); compilable
If your code requires anything complex, I suggest that you add a comment saying which function gets called.
I'm confused by your example too but for a different reason. Maybe someone else can set us both straight. By my thinking:
1 2 3
foo(5, 6); // Wrong. foo(int,int,float) and foo(int,float) are both viable
foo(8); // Seems wrong to me for the same reason as above.
foo(7, 2, 8); // Correct. Only foo(int,int,float) has 3 arguments
main.cpp: In function 'int main()':
main.cpp:13:10: error: call of overloaded 'foo(int)' is ambiguous
foo(8);
^
main.cpp:3:6: note: candidate: 'void foo(int, int, float)'
void foo(int a, int b = 10, float c = 0.0f);
^~~
main.cpp:4:6: note: candidate: 'void foo(int, float)'
void foo(int a, float b = 3.14f);
^~~
I recommend not making overloads where common automatic type conversions can be misunderstood. Its possible to do it and get it right, but the next user of the code may struggle with it, and it is easy to call the wrong version and not know it in some cases, as shown here.
There are many ways to avoid this problem, if that turns out to be the best way...
Okay, so it looks like foo(5,6) works because it matches foo(int a, int b=10, float c=0.0f) without needed to convert either of the arguments. To call foo(int, float) it would have to convert 6 to a float.
From the link I posted above:
Best viable function
...
F1 is determined to be a better function than F2 if implicit conversions for all arguments of F1 are not worse than the implicit conversions for all arguments of F2, and
1) there is at least one argument of F1 whose implicit conversion is better than the corresponding implicit conversion for that argument of F2