Beginner problem: casting in opCmp override

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jul 8 15:03:20 PDT 2013


On Mon, Jul 08, 2013 at 02:42:30PM -0700, Jonathan M Davis wrote:
> On Monday, July 08, 2013 23:31:14 Ugbar Ikenaki wrote:
> > Also…Rat is a struct, not a class. Herein might lie the problem.
> 
> So, this is Rat's opCmp, correct? If it's a struct, it makes no sense
> for it to take an Object. Object is the base class for all classes and
> has nothing to do with structs. opCmp needs to take Rat, not Object.
> And to be fully functional, you might need to have a ref overload as
> well (since sometimes the compiler and runtime get overly picky about
> exact signatures - which is slowly getting fixed fortunately). So,
> something like this should work:
> 
> int opCmp(Rat other)
> {
>     return this.opCmp(other);
> }
> 
> int opCmp(ref Rat other)
[...]

The second overload above should be:

	int opCmp(ref const Rat other) const

otherwise the typeinfo of Rat will be wrong, and using Rat in AA's will
not work correctly. (DMD is very picky about the exact signature of
opCmp when it's generating typeinfos -- as I found out yesterday.)


T

-- 
I think the conspiracy theorists are out to get us...


More information about the Digitalmars-d-learn mailing list