Operator overloading

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Dec 27 17:01:20 PST 2008


John Reimer wrote:
> Hello Andrei,
> 
>> The Anh Tran wrote:
>>
>>> Andrei Alexandrescu wrote:
>>>
>>>>> auto op(++)(); // bar++
>>>>> auto op(++)(int); // ++bar
>>>> Hey, wasn't the implementation of the postincrement operator through
>>>> an overload a rather untasty hack?
>>>>
>>>> Aside for a minor change in notation, there's no improvement. We're
>>>> looking for much more broad improvements, such as offering the
>>>> ability to overload several operators with only one function.
>>>>
>>>> Andrei
>>>>
>>> How about this:
>>>
>>> Unary op: ++f, ~f, ~f, +f, -f, *f
>>> auto operator(++, --, ~, !, +, -, *)()
>>> {
>>> // posfix is provided by compiler
>>> // and is only used for: foo++ foo--
>>> static if (posfix)
>>> return op(this.value);
>>> else ...
>>> }
>>> Binary op: equality comparison f1 <= f2
>>> bool operator(<, >, <=, >=, ==, !=)(Foo foo)
>>> {
>>> return op(this.value, foo.value);
>>> }
>>> For un-order object, he/she just list 'correct' operator(s) in op()
>>> list. Ex: bool operator(!=, ==)(Foo foo) {}
>>>
>> Why invent new syntax when compile-time strings are already there?
>>
>> auto operator(string op)() if (op == "++" || op == "--")
>> {
>> return mixin(op ~ "this.value");
>> }
>> etc. That, of course, is orthogonal to the semantic equivalences
>> suggested by Don and does not solve fusion.
>>
>> Andrei
>>
> 
> 
> Once again, I'm no expert in this matter, but the compiler-time strings 
> idea looks like a great solution for this.  Was this brought up before? 
> ...because that was kind of what I was thinking when you mentioned a new 
> operator overloading syntax for D.

Only in private conversation between Walter and me. But anyway, let's 
not forget we need to address fusion as well.

Andrei



More information about the Digitalmars-d mailing list