Beginner problem: casting in opCmp override

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jul 8 16:38:16 PDT 2013


On Tue, Jul 09, 2013 at 12:35:32AM +0200, Ugbar Ikenaki wrote:
> Here's one more question:
> 
> Before I knew that opEquals existed, I tried overloading the
> equality expressions (== and !=) in opBinary using the code below.
> It worked. Why would the overloaded opBinary version get called if
> the equality expressions are held in opEquals?
[...]

I doubt opBinary was actually called. Probably what happened was that ==
defaulted to the built-in implementation of opEquals, which simply
performs a bitwise comparison of the struct/class, and it just so
happened that it was close to (or the same) as what you intended.

Basically, when you write x==y, the compiler looks for opEquals and
opCmp. If opEquals is found, then it's rewritten as x.opEquals(y);
otherwise, if opCmp is found, it's rewritten as x.opCmp(y)==0. If
neither are found, then the compiler generates a default implementation
of opEquals, which basically does a bitwise comparison of x and y.


T

-- 
Arise, you prisoners of Windows
Arise, you slaves of Redmond, Wash,
The day and hour soon are coming
When all the IT folks say "Gosh!"
It isn't from a clever lawsuit
That Windowsland will finally fall,
But thousands writing open source code
Like mice who nibble through a wall.
-- The Linux-nationale by Greg Baker


More information about the Digitalmars-d-learn mailing list