The following code compiles: --- int foo(int a, int b=2) { return a + b; } int foo(int a) { return a; } int main() { return foo(12); } --- The interaction between default arguments and overloading doesn't appear to be documented, which should be rectified regardless, but isn't this ambiguous? The above code is rejected by C++ compilers as 'ambiguous', and I would certainly expect an error to be generated in this particular case.
And this Error int foo(int a, int b=2){ return a + b; } int foo(float a){ return a; } void main() { writeln( foo(12) ); }
@Luhrel created dlang/dmd pull request #11127 "Fix Issue 16181 - Overloading doesn't consider default arguments with…" fixing this issue: - Fix Issue 16181 - Overloading doesn't consider default arguments with regards to ambiguity https://github.com/dlang/dmd/pull/11127
The default argument is treated as an argument. While both functions match, then the "least as specialized" rule is applied: foo(a) can call foo(int a, int b = 2) foo(a, 2) cannot call foo(int a) Therefore, foo(int a) is selected. Not a bug.