D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16181 - Overloading doesn't consider default arguments with regards to ambiguity
Summary: Overloading doesn't consider default arguments with regards to ambiguity
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 minor
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2016-06-17 16:54 UTC by Bernard Helyer
Modified: 2020-05-11 04:51 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Bernard Helyer 2016-06-17 16:54:14 UTC
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 Danila Letunovskiy 2016-06-17 20:49:30 UTC
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 Dlang Bot 2020-05-10 21:58:50 UTC
@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 Walter Bright 2020-05-11 04:51:40 UTC
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.