File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -520,17 +520,22 @@ $(SPEC_RUNNABLE_EXAMPLE_RUN
520
520
struct S
521
521
{
522
522
int i, j;
523
- int opCmp(ref const S s) const { return i - s.i; } // ignore j
523
+ int opCmp(ref const S s) const { return (i > s.i) - (i < s.i) ; } // ignore j
524
524
}
525
525
526
526
S a = {2, 3};
527
527
S b = {2, 1};
528
+ S c = {3, 0};
529
+ assert(a < c);
528
530
assert(a <= b);
529
- assert(!(a < b));
530
- assert(a != b); // generated opEquals tests both i and j members
531
+ assert(!(a < b)); // opCmp ignores j
532
+ assert(a != b); // generated opEquals tests both i and j members
531
533
---
532
534
)
533
535
536
+ $(BEST_PRACTICE Using `(i > s.i) - (i < s.i)` instead of `i - s.i` to
537
+ compare integers avoids overflow/underflow.)
538
+
534
539
535
540
$(H2 $(LEGACY_LNAME2 FunctionCall, function-call, Function Call Operator Overloading $(D f())))
536
541
You can’t perform that action at this time.
0 commit comments