Bug 8844 – Warning for bug-prone operator overloading
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-17T15:46:15Z
Last change time
2020-03-21T03:56:35Z
Keywords
diagnostic
Assigned to
No Owner
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-10-17T15:46:15Z
struct Foo {
Foo opBinary(string op="-")(Foo f) {
return Foo();
}
}
void main() {
auto Foo = Foo() + Foo();
}
Compiles and run with no errors, dmd 2.061alpha, because "-" is a default argument for the op template argument, so that code is formally correct. But I'd like a warning here (or an error?), because I think such code is bug-prone.
Comment #1 by andrej.mitrovich — 2012-10-18T11:28:48Z
Technically it's still callable:
auto Foo = Foo().opBinary(Foo());
But I don't know why someone would explicitly call opBinary without operators.
Making it an error could help catch bugs though. +1 from me.
Comment #2 by b2.temp — 2020-02-20T15:05:12Z
This is part of a more general problem that is default template parameter values are not always checked.
*** This issue has been marked as a duplicate of issue 20576 ***