Overloading relational operators separately; thoughts?

Martin Nowak via Digitalmars-d digitalmars-d at puremagic.com
Sat Oct 1 00:13:39 PDT 2016


On Wednesday, 28 September 2016 at 20:16:08 UTC, Walter Bright 
wrote:
> On 9/28/2016 6:58 AM, Timon Gehr wrote:
>>> An excellent example of that is the std.regex package.
>> It's actually a bad example, because it is irrelevant: it is 
>> obviously a bad
>> idea to implement regex using operator overloading, because 
>> the regex operators
>> have no D equivalent.
>
> Yet this "obviously bad" idea regularly resurfaces as a cool 
> use of expression templates and respected people in the 
> industry like it and advocate it.
>
>
>> Assume I have two symbolic expressions a and b:
>>
>> Expression a=variable("a"), b=variable("b");
>>
>> Why should I be allowed to do
>>
>> auto c = a + b; // symbolic value a + b
>>
>> but not
>>
>> auto d = a <= b; // symbolic value a <= b
>
> Because there is no way to stop the former but still have 
> operator overloading.

The fact that it's not possible to overload < but have to use the 
ternary opCmp is even a performance problem. All std algorithms 
only use less as a predicate, leading to lots of unnecessary 
cycles when e.g. sorting UDTs.

While I agree with your point on expression templates, 
overloading comparison operators has valid use cases.



More information about the Digitalmars-d mailing list