Operator overhaul

Denis Koroskin 2korden at gmail.com
Sat Nov 1 13:47:04 PDT 2008


On Sat, 01 Nov 2008 23:42:30 +0300, Stewart Gordon <smjg_1998 at yahoo.com>  
wrote:

> "Christopher Wright" <dhasenan at gmail.com> wrote in message  
> news:gei556$1sgc$3 at digitalmars.com...
> <snip>
>> I can never remember how the values for comparison work. An enum  
>> somewhere (except that's a new type, so just constants):
>>
>> struct Comparison
>> {
>> const int LeftIsGreater = -1;
>> const int Equal = 0;
>> const int RightIsGreater = 1;
>> }
>>
>> That's simple, it doesn't require any compiler changes, and it'll save  
>> me some headaches.
>
> Enums are implicitly convertible to their underlying types, so there's  
> no point in using a struct instead.
>
> If you're taking advantage of the ability to return an arbitrary int for  
> performance, are you planning to multiply by one of these constants?
>
> Stewart.
>

I often use the following pattern:

[code]
     auto possibleResults[3] = [
         Result1,
         Result2,
         Result3,
     ];

     int cmp = compare(a, b) + 1; // cmp is 0, 1 or 2
     return possibleResults[cmp];
[/code]

instead of bunch of ifs.



More information about the Digitalmars-d mailing list