Consider the following code:
void foo(int* = 0); // (1)
void foo(int = 0); // (2)
int main() {
foo(0); // OK, calls (2)
foo(); // error, ambiguous overload
}
Both GCC and clang act like this, but I'm not convinced that it's correct.
[expr.call] p6 merely says:
If there is no corresponding argument, the default argument for the parameter is used.
This suggests that once a function call is made, foo will turn into foo(0) and that 0 will have to be converted to int* for (1).
Therefore, I believe both function calls should be valid. However, is there wording which contradicts this idea? Are conversions of default arguments considered to be "free" so that they don't contribute to the conversion sequence?
Before we can consider something a "function call", the postfix-expression (the identifier before
()) must be resolved from possible overloads (if it's a function name and not e.g. an object). See Note 1 in [expr.call]:And when doing overload resolution, we have the following in [over.match.viable] (emphasis mine):
Your function call has 0 arguments, and both overloads of
foohave 1 default parameter. This falls into 2.3 - the default parameters are truncated from the parameters lists of both candidate functions, and so we end up with 2 viable functions, both with 0 parameters, indistinguishable from each other.