Operator overloading or alternatives to expression templates
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Mon Sep 14 11:23:06 PDT 2015
On 09/13/2015 03:06 PM, Martin Nowak wrote:
> On 09/13/2015 07:16 PM, Daniel N wrote:
>>
>> Could you try this?
>>
>> int opCmp(Foo rhs)
>> {
>> return (id > rhs.id) - (id < rhs.id);
>> }
>
> That's not the point, opCmp requires twice as many comparisons as needed
> for <. If they are more expansive, e.g. string comparison, your trick
> won't work.
Well for string comparisons, the added cost (if not optimized away) is
constant. Overall it seems to me that D is to be appreciated for
requiring only one operator for all ordering comparisons (it causes a
lot of noise in C++). Reducing comparisons is the job of the optimizer.
Expression templates are a possible loss, but of a questionable trick.
-- Andrei
More information about the Digitalmars-d
mailing list