@@ -870,33 +870,42 @@ $(H3 $(LEGACY_LNAME2 floating_point_comparisons, floating-point-comparisons, Flo
870
870
there are other floating-point values for NaN produced at runtime.
871
871
Use $(REF isNaN, std,math,traits) to handle all of them.)
872
872
873
- $(H3 $(LEGACY_LNAME2 class_comparisons, class-comparisons, Class Comparisons))
873
+ $(H3 $(LEGACY_LNAME2 class_comparisons, class-comparisons, Class and Struct Comparisons))
874
874
875
- $(P For class objects, *EqualExpression* and *RelExpression* compare the
876
- *contents* of the objects. Therefore, comparing against
877
- a $(CODE null) class reference is invalid, as $(CODE null) has no contents .)
875
+ $(P For struct objects, a *RelExpression* performs a comparison which first
876
+ evaluates $(DDSUBLINK spec/operatoroverloading, compare, a matching `opCmp`
877
+ method) call .)
878
878
879
- $(SPEC_RUNNABLE_EXAMPLE_COMPILE
879
+ $(P For class references, a *RelExpression* performs a comparison which first
880
+ evaluates to an `int` which is either:)
881
+
882
+ - `0` if the two object references are identical
883
+ - `-1` if the left-hand expression is `null`
884
+ - `1` if the right-hand expression is `null`
885
+ - the result of $(DDSUBLINK spec/operatoroverloading, compare,
886
+ a matching `opCmp`) call
887
+
888
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
880
889
---
881
- class C {}
890
+ class C
891
+ {
892
+ override int opCmp(Object o) { assert(0); }
893
+ }
882
894
883
- void fun ()
895
+ void main ()
884
896
{
885
897
C c;
886
898
//if (c < null) {} // compile-time error
887
899
assert(c is null);
888
- if (c > new C) {} // runtime error
900
+ assert (c < new C); // C.opCmp is not called
889
901
}
890
902
---
891
903
)
892
904
893
- $(P For class objects, the result of `Object.opCmp()` forms the left
894
- operand, and `0` forms the right operand. The result of an
895
- *EqualExpression* or *RelExpression* `(o1 op o2)` is:)
896
-
897
- ---
898
- (o1.opCmp(o2) op 0)
899
- ---
905
+ $(P Secondly, for class and struct objects, the evaluated `int` is compared
906
+ against zero using the given operator, which forms the result of the
907
+ *RelExpression*. For more information, see
908
+ $(DDSUBLINK spec/operatoroverloading, compare, `opCmp`).)
900
909
901
910
902
911
$(H2 $(LNAME2 in_expressions, In Expressions))
0 commit comments