Comparison: when operator<() is better than opCmp()

Don Clugston dac at nospam.com.au
Wed Sep 19 07:46:43 PDT 2007


Giuseppe Bilotta wrote:
> Hello all,
> 
> I'm quite a newbie at D programming, although I have some
> programming experience in languages such as C, C++, Ruby.
> 
> I've been initially attracted to D by its native support for the
> 'real' floating point type with the proper native support for nans
> and infinities, and I've been thinking about doing some serious
> scientific computing stuff in it.

My experience was exactly the same. Welcome to D!

> So what I was wondering was if it was possible to have the
> possibility to overload the single comparison operators in D,
> while still keeping opCmp(). The way I see it, overloading would
> work in a way similar to the way opAdd/opAdd_r work.

I'd have another use for the same thing: expression templates don't work with 
opCmp. Expression templates work by encoding information in the return type. 
This is really easy in D, but unfortunately opCmp() must return an int, so you 
can't encode any information in the return type (same thing applies to ==). If 
the simple operators were available, you could do it. .
OTOH, expression templates could well be a technological dead-end.

I also note that there is no way to overload the NCEG operators. a!<>b is not 
possible for user-defined types.



More information about the Digitalmars-d mailing list