forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operatoroverloading.dd
1247 lines (968 loc) · 26.9 KB
/
operatoroverloading.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ddoc
$(SPEC_S Operator Overloading,
$(V2
$(P Operator overloading is accomplished by rewriting operators whose
operands are class or struct objects into calls to specially named
member functions. No additional syntax is used.
)
$(UL
$(LI $(LINK2 #Unary, Unary Operator Overloading))
$(LI $(LINK2 #Cast, Cast Operator Overloading))
$(LI $(LINK2 #Binary, Binary Operator Overloading))
$(LI $(LINK2 #equals, Overloading == and !=))
$(LI $(LINK2 #compare, Overloading < <=, > and >=))
$(LI $(LINK2 #FunctionCall, Function Call Operator Overloading))
$(LI $(LINK2 #Assignment, Assignment Operator Overloading))
$(LI $(LINK2 #OpAssign, Op Assignment Operator Overloading))
$(LI $(LINK2 #Array, Index Operator Overloading))
$(LI $(LINK2 #Slice, Slice Operator Overloading))
$(LI $(LINK2 #Dispatch, Forwarding))
)
<h2><a name="Unary">Unary Operator Overloading</a></h2>
$(TABLE2 Overloadable Unary Operators,
$(TR $(TH$(I op)) $(TH$(I rewrite)) )
$(TR
$(TD -$(I e))
$(TD $(CODE $(I e).opUnary!("-")()))
)
$(TR
$(TD +$(I e))
$(TD $(CODE $(I e).opUnary!("+")()))
)
$(TR
$(TD ~$(I e))
$(TD $(CODE $(I e).opUnary!("~")()))
)
$(TR
$(TD *$(I e))
$(TD $(CODE $(I e).opUnary!("*")()))
)
$(TR
$(TD ++$(I e))
$(TD $(CODE $(I e).opUnary!("++")()))
)
$(TR
$(TD --$(I e))
$(TD $(CODE $(I e).opUnary!("--")()))
)
)
$(P For example, in order to overload the - (negation) operator for struct S, and
no other operator:)
---
struct S {
int m;
int opUnary(string s)() if (s == "-") {
return -m;
}
}
int foo(S s) {
return -s;
}
---
<h3>Postincrement $(I e)++ and Postdecrement $(I e)-- Operators</h3>
$(P These are not directly overloadable, but instead are rewritten
in terms of the ++$(I e) and --$(I e) prefix operators:
)
$(TABLE2 Postfix Operator Rewrites,
$(TR $(TH$(I op)) $(TH$(I rewrite)) )
$(TR
$(TD $(I e)--)
$(TD $(CODE (auto t = e, --$(I e), t)))
)
$(TR
$(TD $(I e)++)
$(TD $(CODE (auto t = e, ++$(I e), t)))
)
)
<h3>Overloading Index Unary Operators</h3>
$(TABLE2 Overloadable Index Unary Operators,
$(TR $(TH$(I op)) $(TH$(I rewrite)) )
$(TR
$(TD $(CODE -$(I a)[$(ARGUMENTS)]))
$(TD $(CODE $(I a).opIndexUnary!("-")($(ARGUMENTS))))
)
$(TR
$(TD $(CODE +$(I a)[$(ARGUMENTS)]))
$(TD $(CODE $(I a).opIndexUnary!("+")($(ARGUMENTS))))
)
$(TR
$(TD $(CODE ~$(I a)[$(ARGUMENTS)]))
$(TD $(CODE $(I a).opIndexUnary!("~")($(ARGUMENTS))))
)
$(TR
$(TD $(CODE *$(I a)[$(ARGUMENTS)]))
$(TD $(CODE $(I a).opIndexUnary!("*")($(ARGUMENTS))))
)
$(TR
$(TD $(CODE ++$(I a)[$(ARGUMENTS)]))
$(TD $(CODE $(I a).opIndexUnary!("++")($(ARGUMENTS))))
)
$(TR
$(TD $(CODE --$(I a)[$(ARGUMENTS)]))
$(TD $(CODE $(I a).opIndexUnary!("--")($(ARGUMENTS))))
)
)
<h3>Overloading Slice Unary Operators</h3>
$(TABLE2 Overloadable Slice Unary Operators,
$(TR $(TH$(I op)) $(TH$(I rewrite)) )
$(TR
$(TD $(CODE -$(I a)[$(SLICE)]))
$(TD $(CODE $(I a).opSliceUnary!("-")($(SLICE2))))
)
$(TR
$(TD $(CODE +$(I a)[$(SLICE)]))
$(TD $(CODE $(I a).opSliceUnary!("+")($(SLICE2))))
)
$(TR
$(TD $(CODE ~$(I a)[$(SLICE)]))
$(TD $(CODE $(I a).opSliceUnary!("~")($(SLICE2))))
)
$(TR
$(TD $(CODE *$(I a)[$(SLICE)]))
$(TD $(CODE $(I a).opSliceUnary!("*")($(SLICE2))))
)
$(TR
$(TD $(CODE ++$(I a)[$(SLICE)]))
$(TD $(CODE $(I a).opSliceUnary!("++")($(SLICE2))))
)
$(TR
$(TD $(CODE --$(I a)[$(SLICE)]))
$(TD $(CODE $(I a).opSliceUnary!("--")($(SLICE2))))
)
$(TR
$(TD $(CODE -$(I a)[ ]))
$(TD $(CODE $(I a).opSliceUnary!("-")()))
)
$(TR
$(TD $(CODE +$(I a)[ ]))
$(TD $(CODE $(I a).opSliceUnary!("+")()))
)
$(TR
$(TD $(CODE ~$(I a)[ ]))
$(TD $(CODE $(I a).opSliceUnary!("~")()))
)
$(TR
$(TD $(CODE *$(I a)[ ]))
$(TD $(CODE $(I a).opSliceUnary!("*")()))
)
$(TR
$(TD $(CODE ++$(I a)[ ]))
$(TD $(CODE $(I a).opSliceUnary!("++")()))
)
$(TR
$(TD $(CODE --$(I a)[ ]))
$(TD $(CODE $(I a).opSliceUnary!("--")()))
)
)
<h2><a name="Cast">Cast Operator Overloading</a></h2>
$(TABLE2 Cast Operators,
$(TR $(TH$(I op)) $(TH$(I rewrite)) )
$(TR
$(TD cast($(I type))$(I e))
$(TD $(CODE $(I e).opCast!($(I type))()))
)
)
<h3>Boolean Operations</h3>
$(P Notably absent from the list of overloaded unary operators is the !
logical negation operator. More obscurely absent is a unary operator
to convert to a bool result.
Instead, these are covered by a rewrite to:
)
---
opCast!(bool)(e)
---
$(P So,)
---
if (e) => if (e.opCast!(bool))
if (!e) => if (!e.opCast!(bool))
---
$(P etc., whenever a bool result is expected. This only happens, however, for
instances of structs. Class references are converted to bool by checking to
see if the class reference is null or not.
)
<h2><a name="Binary">Binary Operator Overloading</a></h2>
$(P The following binary operators are overloadable:)
$(TABLE2 Overloadable Binary Operators,
$(TR $(TD +) $(TD -) $(TD *) $(TD /) $(TD %) $(TD ^^) $(TD &) )
$(TR $(TD |) $(TD ^) $(TD <<) $(TD >>) $(TD >>>) $(TD ~) $(TD in) )
)
$(P The expression:)
---
a $(I op) b
---
$(P is rewritten as both:)
---
a.opBinary!("$(I op)")(b)
b.opBinaryRight!("$(I op)")(a)
---
$(P and the one with the $(SINGLEQUOTE better) match is selected.
It is an error for both to equally match.
)
<h2><a name="equals">Overloading == and !=</a></h2>
$(P Expressions of the form $(CODE a != b) are rewritten as $(CODE !(a == b)).)
$(P Given $(CODE a == b) :)
$(OL
$(LI If a and b are both class objects, then the expression is rewritten as:
---
.object.opEquals(a, b)
---
and that function is implemented as:
---
bool opEquals(Object a, Object b) {
if (a is b) return true;
if (a is null || b is null) return false;
if (typeid(a) == typeid(b)) return a.opEquals(b);
return a.opEquals(b) && b.opEquals(a);
}
---
)
$(LI Otherwise the expressions $(CODE a.opEquals(b)) and
$(CODE b.opEquals(a)) are tried. If both resolve to the same opEquals
function, then the expression is rewritten to be $(CODE a.opEquals(b)).
)
$(LI If one is a better match then the other, or one compiles and the other
does not, the one is selected.)
$(LI Otherwise, an error results.)
)
$(P If overridding Object.opEquals() for classes, the class member function
signature should look like:)
---
class C {
override bool opEquals(Object o) { ... }
}
---
$(P If structs declare an opEquals member function, it should follow the following
form:)
---
struct S {
int opEquals(ref const S s) { ... }
}
---
<h2><a name="compare">Overloading < <=, > and >=</a></h2>
$(P Comparison operations are rewritten as follows:)
$(TABLE2 Overloadable Unary Operators,
$(TR $(TH comparison) $(TH rewrite 1) $(TH rewrite 2) )
$(TR $(TD $(CODE a < b)) $(TD $(CODE a.opCmp(b) < 0)) $(TD $(CODE b.opCmp(a) > 0)))
$(TR $(TD $(CODE a <= b)) $(TD $(CODE a.opCmp(b) <= 0)) $(TD $(CODE b.opCmp(a) >= 0)))
$(TR $(TD $(CODE a > b)) $(TD $(CODE a.opCmp(b) > 0)) $(TD $(CODE b.opCmp(a) < 0)))
$(TR $(TD $(CODE a >= b)) $(TD $(CODE a.opCmp(b) >= 0)) $(TD $(CODE b.opCmp(a) <= 0)))
)
$(P Both rewrites are tried. If only one compiles, that one is taken.
If they both resolve to the same function, the first
rewrite is done. If they resolve to different functions, the best matching one
is used. If they both match the same, but are different functions, an ambiguity
error results.
)
$(P If overriding Object.opCmp() for classes, the class member function
signature should look like:)
---
class C {
override int opCmp(Object o) { ... }
}
---
$(P If structs declare an opCmp member function, it should follow the following
form:)
---
struct S {
int opCmp(ref const S s) { ... }
}
---
<h2><a name="FunctionCall">Function Call Operator Overloading $(I f)()</a></h2>
$(P The function call operator, (), can be overloaded by
declaring a function named $(B opCall):
)
-------
struct F
{
int $(B opCall)();
int $(B opCall)(int x, int y, int z);
}
void test()
{ F f;
int i;
i = f$(B ()); // same as i = f.opCall();
i = f$(B (3,4,5)); // same as i = f.opCall(3,4,5);
}
-------
$(P In this way a struct or class object can behave as if it
were a function.
)
<h2><a name="Assignment">Assignment Operator Overloading</a></h2>
$(P The assignment operator $(CODE =) can be overloaded if the
lvalue is a struct aggregate, and $(CODE opAssign)
is a member function of that aggregate.)
$(P The assignment operator cannot be overloaded for rvalues
that can be implicitly cast to the lvalue type.
Furthermore, the following parameter signatures for $(CODE opAssign)
are not allowed:)
---
opAssign(...)
opAssign(T)
opAssign(T, ...)
opAssign(T ...)
opAssign(T, U = defaultValue, etc.)
---
$(P where $(I T) is the same type as the aggregate type $(I A),
is implicitly
convertible to $(I A), or if $(I A) is a struct and $(I T)
is a pointer to a type that is
implicitly convertible to $(I A).
)
<h3>Index Assignment Operator Overloading</h2>
$(P If the left hand side of an assignment is an index operation
on a struct or class instance,
it can be overloaded by providing an opIndexAssign member function.
Expressions of the form $(CODE a[$(ARGUMENTS)] = c) are rewritten
as $(CODE a.opIndexAssign(c, $(ARGUMENTS))).
)
-------
struct A
{
int $(B opIndexAssign)(int value, size_t i1, size_t i2);
}
void test()
{ A a;
a$(B [)i,3$(B ]) = 7; // same as a.opIndexAssign(7,i,3);
}
-------
<h3>Slice Assignment Operator Overloading</h2>
$(P If the left hand side of an assignment is a slice operation
on a struct or class instance,
it can be overloaded by providing an opSliceAssign member function.
Expressions of the form $(CODE a[$(SLICE)] = c) are rewritten
as $(CODE a.opSliceAssign(c, $(SLICE2))), and
$(CODE a[] = c) as $(CODE a.opSliceAssign(c)).
)
-------
struct A
{
int $(B opSliceAssign)(int v); // overloads a[] = v
int $(B opSliceAssign)(int v, size_t x, size_t y); // overloads a[i .. j] = v
}
void test()
{ A a;
int v;
a$(B []) = v; // same as a.opSliceAssign(v);
a$(B [)3..4$(B ]) = v; // same as a.opSliceAssign(v,3,4);
}
-------
<h2><a name="OpAssign">Op Assignment Operator Overloading</a></h2>
$(P The following op assignment operators are overloadable:)
$(TABLE2 Overloadable Op Assignment Operators,
$(TR $(TD +=) $(TD -=) $(TD *=) $(TD /=) $(TD %=) $(TD ^^=) $(TD &=) )
$(TR $(TD |=) $(TD ^=) $(TD <<=) $(TD >>=) $(TD >>>=) $(TD ~=) $(TD ) )
)
$(P The expression:)
---
a $(I op)= b
---
$(P is rewritten as:)
---
a.opOpAssign!("$(I op)")(b)
---
<h3>Index Op Assignment Operator Overloading</h3>
$(P If the left hand side of an $(I op)= is an index expression on
a struct or class instance and opIndexOpAssign is a member:)
---
a[$(ARGUMENTS)] $(I op)= c
---
$(P it is rewritten as:)
---
a.opIndexOpAssign!("$(I op)")(c, $(ARGUMENTS))
---
<h3>Slice Op Assignment Operator Overloading</h3>
$(P If the left hand side of an $(I op)= is a slice expression on
a struct or class instance and opSliceOpAssign is a member:)
---
a[$(SLICE)] $(I op)= c
---
$(P it is rewritten as:)
---
a.opSliceOpAssign!("$(I op)")(c, $(SLICE2))
---
$(P and)
---
a[] $(I op)= c
---
$(P it is rewritten as:)
---
a.opSliceOpAssign!("$(I op)")(c)
---
<h2><a name="Array">Index Operator Overloading</a></h2>
$(P The array index operator, $(CODE a[$(ARGUMENTS)]), can be overloaded by
declaring a function named $(B opIndex) with one
or more parameters.
)
-------
struct A
{
int $(B opIndex)(size_t i1, size_t i2, size_t i3);
}
void test()
{ A a;
int i;
i = a$(B [)5,6,7$(B ]); // same as i = a.opIndex(5,6,7);
}
-------
$(P In this way a struct or class object can behave as if it
were an array.
)
$(P If an index expression can be rewritten using opIndexAssign or opIndexOpAssign,
those are preferred over opIndex.
)
<h2><a name="Slice">Slice Operator Overloading</a></h2>
$(P Overloading the slicing operator means overloading expressions
like $(CODE a[]) and $(CODE a[$(SLICE)]).
This can be done by declaring a member function named $(B opSlice).
)
-------
class A
{
int $(B opSlice)(); // overloads a[]
int $(B opSlice)(size_t x, size_t y); // overloads a[i .. j]
}
void test()
{ A a = new A();
int i;
int v;
i = a$(B []); // same as i = a.opSlice();
i = a$(B [)3..4$(B ]); // same as i = a.opSlice(3,4);
}
-------
$(P If a slice expression can be rewritten using opSliceAssign or opSliceOpAssign,
those are preferred over opSlice.
)
<h2><a name="Dispatch">Forwarding</a></h2>
$(P Member names not found in a class or struct can be forwarded
to a template function named $(CODE opDispatch) for resolution.
)
---
import std.stdio;
struct S
{
void opDispatch(string s, T)(T i)
{
writefln("S.opDispatch('%s', %s)", s, i);
}
}
class C
{
void opDispatch(string s)(int i)
{
writefln("C.opDispatch('%s', %s)", s, i);
}
}
struct D
{
template opDispatch(string s)
{
enum int opDispatch = 8;
}
}
void main()
{
S s;
s.opDispatch!("hello")(7);
s.foo(7);
auto c = new C();
c.foo(8);
D d;
writefln("d.foo = %s", d.foo);
assert(d.foo == 8);
}
---
)
$(V1
$(P Overloading is accomplished by interpreting specially named
struct and class member functions as being implementations of unary and
binary operators. No additional syntax is used.
)
$(UL
$(LI $(LINK2 #Unary, Unary Operator Overloading))
$(LI $(LINK2 #Binary, Binary Operator Overloading))
$(LI $(LINK2 #FunctionCall, Function Call Operator Overloading))
$(LI $(LINK2 #Array, Array Operator Overloading))
$(LI $(LINK2 #Assignment, Assignment Operator Overloading))
$(V2
$(DOT $(LI $(LINK2 #Dot, Forwarding)))
)
$(LI $(LINK2 #Future, Future Directions))
)
<h2><a name="Unary">Unary Operator Overloading</a></h2>
$(TABLE2 Overloadable Unary Operators,
$(TR $(TH$(I op)) $(TH$(I opfunc)) )
$(TR
$(TD -$(I e))
$(TD $(CODE opNeg))
)
$(TR
$(TD +$(I e))
$(TD $(CODE opPos))
)
$(TR
$(TD ~$(I e))
$(TD $(CODE opCom))
)
$(TR
$(TD $(I e)++)
$(TD $(CODE opPostInc))
)
$(TR
$(TD $(I e)--)
$(TD $(CODE opPostDec))
)
$(TR
$(TD cast($(I type))$(I e))
$(TD $(CODE opCast))
)
)
$(P Given a unary
overloadable operator $(I op) and its corresponding
class or struct member
function name $(I opfunc), the syntax:
)
---
$(I op) a
---
$(P where $(I a) is a class or struct object reference,
is interpreted as if it was written as:
)
---
a.$(I opfunc)()
---
<h3>Overloading ++$(I e) and --$(I e)</h3>
$(P Since ++$(I e) is defined to be semantically equivalent
to ($(I e) += 1), the expression ++$(I e) is rewritten
as ($(I e) += 1), and then checking for operator overloading
is done. The situation is analogous for --$(I e).
)
<h3>Examples</h3>
$(OL
$(LI
-------
class A { int $(B opNeg)(); }
A a;
-a; // equivalent to a.opNeg();
-------
)
$(LI
-------
class A { int $(B opNeg)(int i); }
A a;
-a; // equivalent to a.opNeg(), which is an error
-------
)
)
<h3>Overloading cast($(I type))$(I e)</h3>
$(P The member function $(I e).$(B opCast()) is called,
and the return value of $(B opCast()) is implicitly converted
to $(I type). Since functions cannot be overloaded based on
return value, there can be only one $(B opCast) per struct or
class.
Overloading the cast operator does not affect implicit casts, it
only applies to explicit casts.
)
-------
struct A
{
int $(B opCast)() { return 28; }
}
void test()
{
A a;
long i = cast(long)a; // i is set to 28L
void* p = cast(void*)a; // error, cannot implicitly
// convert int to void*
int j = a; // error, cannot implicitly convert
// A to int
}
-------
<h2><a name="Binary">Binary Operator Overloading</a></h2>
$(TABLE2 Overloadable Binary Operators,
$(TR $(TH $(I op))
$(TH commutative?)
$(TH $(I opfunc))
$(TH $(I opfunc_r))
)
$(TR $(TD +) $(TD yes) $(TD $(CODE opAdd)) $(TD $(CODE opAdd_r)))
$(TR $(TD -) $(TD no) $(TD $(CODE opSub)) $(TD $(CODE opSub_r)))
$(TR $(TD *) $(TD yes) $(TD $(CODE opMul)) $(TD $(CODE opMul_r)))
$(TR $(TD /) $(TD no) $(TD $(CODE opDiv)) $(TD $(CODE opDiv_r)))
$(TR $(TD %) $(TD no) $(TD $(CODE opMod)) $(TD $(CODE opMod_r)))
$(TR $(TD &) $(TD yes) $(TD $(CODE opAnd)) $(TD $(CODE opAnd_r)))
$(TR $(TD |) $(TD yes) $(TD $(CODE opOr)) $(TD $(CODE opOr_r)))
$(TR $(TD ^) $(TD yes) $(TD $(CODE opXor)) $(TD $(CODE opXor_r)))
$(TR $(TD <<) $(TD no) $(TD $(CODE opShl)) $(TD $(CODE opShl_r)))
$(TR $(TD >>) $(TD no) $(TD $(CODE opShr)) $(TD $(CODE opShr_r)))
$(TR $(TD >>>) $(TD no) $(TD $(CODE opUShr)) $(TD $(CODE opUShr_r)))
$(TR $(TD ~) $(TD no) $(TD $(CODE opCat)) $(TD $(CODE opCat_r)))
$(TR $(TD ==) $(TD yes) $(TD $(CODE opEquals)) $(TD -))
$(TR $(TD !=) $(TD yes) $(TD $(CODE opEquals)) $(TD -))
$(TR $(TD <) $(TD yes) $(TD $(CODE opCmp)) $(TD -))
$(TR $(TD <=) $(TD yes) $(TD $(CODE opCmp)) $(TD -))
$(TR $(TD >) $(TD yes) $(TD $(CODE opCmp)) $(TD -))
$(TR $(TD >=) $(TD yes) $(TD $(CODE opCmp)) $(TD -))
$(TR $(TD =) $(TD no ) $(TD $(CODE opAssign)) $(TD -) )
$(TR $(TD +=) $(TD no) $(TD $(CODE opAddAssign)) $(TD -))
$(TR $(TD -=) $(TD no) $(TD $(CODE opSubAssign)) $(TD -))
$(TR $(TD *=) $(TD no) $(TD $(CODE opMulAssign)) $(TD -))
$(TR $(TD /=) $(TD no) $(TD $(CODE opDivAssign)) $(TD -))
$(TR $(TD %=) $(TD no) $(TD $(CODE opModAssign)) $(TD -))
$(TR $(TD &=) $(TD no) $(TD $(CODE opAndAssign)) $(TD -))
$(TR $(TD |=) $(TD no) $(TD $(CODE opOrAssign)) $(TD -))
$(TR $(TD ^=) $(TD no) $(TD $(CODE opXorAssign)) $(TD -))
$(TR $(TD <<=) $(TD no) $(TD $(CODE opShlAssign)) $(TD -))
$(TR $(TD >>=) $(TD no) $(TD $(CODE opShrAssign)) $(TD -))
$(TR $(TD >>>=) $(TD no) $(TD $(CODE opUShrAssign)) $(TD -))
$(TR $(TD ~=) $(TD no) $(TD $(CODE opCatAssign)) $(TD -))
$(TR $(TD in ) $(TD no ) $(TD $(CODE opIn) ) $(TD $(CODE opIn_r) ))
)
$(P Given a binary
overloadable operator $(I op) and its corresponding
class or struct member
function name $(I opfunc) and $(I opfunc_r),
and the syntax:
)
---
a $(I op) b
---
the following sequence of rules is applied, in order, to determine
which form is used:
$(OL
$(LI The expression is rewritten as both:
---
a.$(I opfunc)(b)
b.$(I opfunc_r)(a)
---
If any $(I a.opfunc) or $(I b.opfunc_r) functions exist,
then overloading is applied
across all of them and the best match is used. If either exist,
and there is no argument match, then it is an error.
)
$(LI If the operator is commutative, then the following
forms are tried:
---
a.$(I opfunc_r)(b)
b.$(I opfunc)(a)
---
)
$(LI If $(I a) or $(I b) is a struct or class object reference,
it is an error.
)
)
<h4>Examples</h4>
$(OL
$(LI
-------
class A { int $(B opAdd)(int i); }
A a;
a + 1; // equivalent to a.opAdd(1)
1 + a; // equivalent to a.opAdd(1)
-------
)
$(LI
-------
class B { int $(B opDiv_r)(int i); }
B b;
1 / b; // equivalent to b.opDiv_r(1)
-------
)
$(LI
-------
class A { int $(B opAdd)(int i); }
class B { int $(B opAdd_r)(A a); }
A a;
B b;
a + 1; // equivalent to a.opAdd(1)
a + b; // equivalent to b.opAdd_r(a)
b + a; // equivalent to b.opAdd_r(a)
-------
)
$(LI
-------
class A { int $(B opAdd)(B b); int $(B opAdd_r)(B b); }
class B { }
A a;
B b;
a + b; // equivalent to a.opAdd(b)
b + a; // equivalent to a.opAdd_r(b)
-------
)
$(LI
-------
class A { int $(B opAdd)(B b); int $(B opAdd_r)(B b); }
class B { int $(B opAdd_r)(A a); }
A a;
B b;
a + b; // ambiguous: a.opAdd(b) or b.opAdd_r(a)
b + a; // equivalent to a.opAdd_r(b)
-------
)
)
<h3>Overloading == and !=</h3>
$(P Both operators use the $(CODE $(B opEquals)()) function.
The expression
$(CODE (a == b)) is rewritten as $(CODE a.$(B opEquals)(b)),
and $(CODE (a != b)) is rewritten as $(CODE !a.$(B opEquals)(b)).
)
$(P The member function $(CODE $(B opEquals)()) is defined as part of
Object as:
)
-------
$(V1 int)$(V2 bool) $(B opEquals)(Object o);
-------
$(P so that every class object has a default $(CODE $(B opEquals)()).
But every class definition which will be using == or != should
expect to need to override opEquals. The parameter to the overriding
function must be of type $(CODE Object), not the type for the class.
)
$(P Structs and unions (hereafter just called structs) can
provide a member function:
)
-------
$(V1 int)$(V2 bool) $(B opEquals)(S s)
-------
$(P or:)
-------
$(V1 int)$(V2 bool) $(B opEquals)(S* s)
-------
$(P where $(CODE S) is the struct name, to define how equality is
determined.)
$(P If a struct has no $(B opEquals) function declared for it,
a bit compare of the contents of the two structs is done to
determine equality or inequality.
)
$(P $(B Note:) Comparing a reference to a class object against $(B null)
should be done as:
)
-------
if (a is null)
-------
$(P and not as:)
-------
if (a == null)
-------
$(P The latter is converted to:)
-------
if (a.$(B opEquals)(null))
-------
$(P which will fail if $(CODE $(B opEquals)()) is a virtual function.)
<h3>Overloading <, <=, > and >=</h3>
$(P These comparison operators all use the $(CODE $(B opCmp)()) function.
The expression
$(CODE (a $(I op) b)) is rewritten as $(CODE (a.$(B opCmp)(b) $(I op) 0)).
The commutative operation is rewritten as $(CODE (0 $(I op) b.$(B opCmp)(a)))
)
$(P The member function $(CODE $(B opCmp)()) is defined as part of Object
as:
)
-------
int $(B opCmp)(Object o);
-------
$(P so that every class object has a $(CODE $(B opCmp)()).
)
$(P $(CODE $(B opCmp)) for structs works analogously to
$(CODE $(B opEquals)) for structs:
)