opCmp with template type not working.

user1234 user1234 at 12.de
Tue Nov 3 03:07:14 UTC 2020


On Tuesday, 3 November 2020 at 02:55:46 UTC, Jonathan Levi wrote:
> This is not working as expected:
>> bool opCmp(string op, U)(U rhs) {
>
> As it does work with:
>> auto opBinary(string op, U)(U rhs) {
>
>
> With:
>
>> struct X {
>>     int x;
>>     auto opBinary(string op, U)(U rhs) {
>>             return mixin("x "~op~" rhs");
>>         }
>>     bool opCmp(string op, U)(U rhs) {
>>         return mixin("x "~op~" rhs");
>>     }
>> }
>
> This compiles:
>> X(5) + 4
> This does not:
>> X(5) < 4
>
> Full example code: https://run.dlang.io/is/Beflkt
>
> Is this a Bug?

opCmp should return a signed integer. This allows to match well 
with the x86 flag register and do fast comparison with integers 
types (in which case just substract rhs to lhs and you're good)


More information about the Digitalmars-d mailing list