Bug 16181 – Overloading doesn't consider default arguments with regards to ambiguity

Status
RESOLVED
Resolution
INVALID
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-06-17T16:54:14Z
Last change time
2020-05-11T04:51:40Z
Keywords
pull
Assigned to
No Owner
Creator
Bernard Helyer

Comments

Comment #0 by b.helyer — 2016-06-17T16:54:14Z
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.
Comment #1 by kapblc — 2016-06-17T20:49:30Z
And this Error int foo(int a, int b=2){ return a + b; } int foo(float a){ return a; } void main() { writeln( foo(12) ); }
Comment #2 by dlang-bot — 2020-05-10T21:58:50Z
@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
Comment #3 by bugzilla — 2020-05-11T04:51:40Z
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.