www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Inconsistent type expectations of struct opEquals and opCmp

Using DMD 0.110, Windows 98SE.

For some strange reason, for a struct to be used as an AA key, the 
opEquals and opCmp functions have to be defined with a pointer 
parameter.  However, when this is done, the comparison operators can no 
longer be used directly.  For both to work, it is necessary to define 
two versions of each comparison operator:

struct Qwert {
     char yuiop;

     int opEquals(Qwert* nm) {
         return yuiop == nm.yuiop;

     int opEquals(Qwert nm) {
         return opEquals(&nm);

     int opCmp(Qwert* nm) {
         return yuiop - nm.yuiop;

     int opCmp(Qwert nm) {
         return opCmp(&nm);

void main() {
     int[Qwert] qaz;

     Qwert asdfg, hjkl, zxcvb;

     asdfg.yuiop = hjkl.yuiop = 42;
     zxcvb.yuiop = 69;

     assert(asdfg == hjkl);
     assert(asdfg != zxcvb);
     assert(hjkl < zxcvb);

     qaz[asdfg] = 20;
     assert (qaz[asdfg] == 20);

If I get rid of opEquals(Qwert) and opCmp(Qwert), then needless to say, 
I get

D:\My Documents\Programming\D\Tests\bugs\struct_equals.d(30): function 
struct_equals.Qwert.opEquals (Qwert *q) does not match argument types 
(Qwert )
D:\My Documents\Programming\D\Tests\bugs\struct_equals.d(30): cannot 
implicitly convert expression hjkl of type Qwert to Qwert *

and similarly with the next two asserts.  OTOH, if I only define 
opEquals(Qwert) and opCmp(Qwert), then

D:\My Documents\Programming\D\Tests\bugs\struct_equals.d(4): function 
struct_equals.Qwert.opEquals must be declared as extern (D) int 
D:\My Documents\Programming\D\Tests\bugs\struct_equals.d(12): function 
struct_equals.Qwert.opCmp must be declared as extern (D) int opCmp(Qwert*)

Having to define both opEquals(Qwert) and opEquals(Qwert*) (and 
similarly with opCmp) seems an arbitrary requirement.  Surely you should 
only have to define one?  And surely someone who designs a struct 
shouldn't have to explicitly make it usable as an AA key?


My e-mail is valid but not my primary mailbox.  Please keep replies on 
the 'group where everyone may benefit.
Jan 06 2005