Skip to content

Commit 0f85081

Browse files
authored
Merge pull request #3474 from RazvanN7/Issue_23553
Fix Issue 23553 - opCmp spec is incomplete
2 parents ee7c040 + 48263a3 commit 0f85081

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

spec/operatoroverloading.dd

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,38 @@ $(H3 $(LNAME2 compare, Overloading $(D <), $(D <)$(D =), $(D >), and $(D >)$(D =
488488
If they resolve to different functions, the best matching one is used.
489489
If they both match the same, but are different functions, an ambiguity
490490
error results.)
491+
---
492+
struct B
493+
{
494+
int opCmp(int) { return -1; }
495+
int opCmp(ref const S) { return -1; }
496+
int opCmp(ref const C) { return -1; }
497+
}
498+
499+
struct S
500+
{
501+
int opCmp(ref const S) { return 1; }
502+
int opCmp(ref B) { return 0; }
503+
}
491504

505+
struct C
506+
{
507+
int opCmp(ref const B) { return 0; }
508+
}
509+
510+
void main()
511+
{
512+
S s;
513+
const S cs;
514+
B b;
515+
C c;
516+
assert(s > s); // s.opCmp(s) > 0
517+
assert(!(s < b)); // s.opCmp(b) > 0 - S.opCmp(ref B) is exact match
518+
assert(!(b < s)); // s.opCmp(b) < 0 - S.opCmp(ref B) is exact match
519+
assert(b < cs); // b.opCmp(s) < 0 - B.opCmp(ref const S) is exact match
520+
static assert(!__traits(compiles, b < c)); // both C.opCmp and B.opcmp match exactly
521+
}
522+
---
492523
$(P If overriding $(D Object.opCmp()) for classes, the class member
493524
function signature should look like:)
494525
---

0 commit comments

Comments
 (0)