Skip to content

Commit 48263a3

Browse files
committed
Fix Issue 23553 - opCmp spec is incomplete
1 parent 54bcf8b commit 48263a3

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
@@ -487,7 +487,38 @@ $(H3 $(LNAME2 compare, Overloading $(D <), $(D <)$(D =), $(D >), and $(D >)$(D =
487487
If they resolve to different functions, the best matching one is used.
488488
If they both match the same, but are different functions, an ambiguity
489489
error results.)
490+
---
491+
struct B
492+
{
493+
int opCmp(int) { return -1; }
494+
int opCmp(ref const S) { return -1; }
495+
int opCmp(ref const C) { return -1; }
496+
}
497+
498+
struct S
499+
{
500+
int opCmp(ref const S) { return 1; }
501+
int opCmp(ref B) { return 0; }
502+
}
490503

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

0 commit comments

Comments
 (0)