Operator overloading for size_t

Alec Stewart alec-stewart at protonmail.com
Thu Mar 14 19:39:53 UTC 2019


On Thursday, 14 March 2019 at 18:25:17 UTC, H. S. Teoh wrote:
> On Thu, Mar 14, 2019 at 06:07:46PM +0000, Alec Stewart via 
> Digitalmars-d-learn wrote: [...]
>>     bool opEquals(ref const Interval i) const {
>>         // probably would be a bit more than just this, but 
>> for this issue
>>         // let's just stick with this.
>>         return d_start.opEquals(other.d_start) && 
>> d_end.opEquals(other.d_end);
>>     }
>
> There's no need to call opEquals explicitly like that. All you 
> need to do is to use <, ==, and > as you normally would:
>
>      bool opEquals(ref const Interval i) const {
>          return d_start == other.d_start) && d_end == d_end;
>      }
>
>
> T

Thanks. I somehow managed to overthink this...

For < and >, would one do this?

     size_t opCmp(ref const Interval other) const {
         return d_start < other.d_start;
     }

     size_t opCmp(ref const Interval other) const {
         return d_end < other.d_end;
     }

     size_t opCmp(ref const Interval other) const {
         return d_start > other.d_start;
     }

     size_t opCmp(ref const Interval other) const {
         return d_end > other.d_end;
     }

Or would it better to do

     size_t opCmp(ref const Interval other) const {
         if (d_start < other.d_start) {
             return d_start < other.d_start;
         } else if (d_start > other.d_start) {
             return d_start > other.d_start;
         } else if (d_end < other.d_end) {
             return d_end < other.d_end;
         } else if (d_end > other.d_end) {
             return d_end > other.d_end;
         } else {
             return false;
         }
     }




More information about the Digitalmars-d-learn mailing list