user defined implicit casts

Reiner Pope some at address.com
Mon Jul 30 19:51:39 PDT 2007


Jarrett Billingsley wrote:
> "Ender KaShae" <astrothayne at gmail.ckom> wrote in message 
> news:f8m0k6$2oo9$1 at digitalmars.com...
>> c++ has member, global, and static operator overloads it is up to the 
>> programmer to choose how to implement it, I think it would be nice if D 
>> had a similar mechanizm.
> 
> Can you come up with something more solid than "this is how C++ does it?" 
> Can you explain what functionality global and static operator overloads 
> provide that member operator overloads cannot? 
> 
> 
Non-member operator overloads are useful for the same reason that 
non-member functions are useful: writing functions that operate on a 
type, but don't belong within that type.

I can think of one example where global operator overloads make sense:

// stores a list of T, with opIndex, opCat, etc
class List(T) { ... }

In this case it might be reasonable to define a function which does a 
elementwise add of two List!(int)s to produce a third:

List!(int) opAdd(List!(int) a, List!(int) b) { ... }

But this operator clearly doesn't belong in the List class, as in the 
general case, you can't add two lists together.


   -- Reiner



More information about the Digitalmars-d mailing list