Overloading + on points

Simen kjaeraas simen.kjaras at gmail.com
Thu Sep 2 08:15:04 PDT 2010


Eduardo Cavazos <wayo.cavazos at gmail.com> wrote:

>> Pt opBinary( string op : "+", T : Pt )( T a ) {...}
>> Pt opBinary( string op : "+", T : float )( T a ) {...}

>> Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == Pt ) )
>> {...}
>> Pt opBinary( string op, T )( T a ) if ( ( op == "+" ) && is( T == float  
>> )
>> ) {...}
>
> Is either one of those solutions preferrable? Or are they equivalent?  
> I'd like to get these right now since there are a bunch more like this  
> (for all the various operators and combinations).

They are not entirely equivalent. The first two use T : float, which is
a test of implicit castability, while is( T == float ) is a test for
equality. IOW, the second set would barf on receiving a non-float
parameter, like double or int. For some reason I messed that up. If
instead I had written the tests as is( T : Pt ) and is( T : float ),
they would be equivalent.

string op : "+" basically means the same as the template constraint
if ( op == "+" ). The latter is more flexible, as you can compare to more
values than the singular value in the op : "+".

-- 
Simen


More information about the Digitalmars-d mailing list