Operator overhaul
Christopher Wright
dhasenan at gmail.com
Sat Nov 1 14:59:19 PDT 2008
Stewart Gordon 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.
I didn't realize that.
> 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?
This would mainly be for writing opCmp, and as documentation. As an
alternative:
struct Comparison
{
static Comparison LeftIsGreater = {-1};
static Comparison Equal = {0};
static Comparison RightIsGreater = {1};
int value;
bool opEquals(int i)
{
if (value == 0 && i == 0) return true;
if (value < 0 && i < 0) return true;
if (value > 0 && i > 0) return true;
return false;
}
}
More information about the Digitalmars-d
mailing list