Operator overloading problem

div0 div0 at sourceforge.net
Fri Aug 6 13:49:18 PDT 2010


On 06/08/2010 21:37, div0 wrote:
>
>
> You need to add a second template parameter for the function arguments
> and add a template constrait like so:
>
> struct Group {
>     int i1;
>
>     Group opBinary(string op, U) (U x)
>     if(op == "+" && is(U: int))
>     {
> 	// do somehting
> 	return this;
>     }
>
>     Group opBinary(string op, U) (U rhs)
>     if(op == "+" && is(U: Group))
>     {
> 	// do something
> 	return this;
>     }
> }

And if you are old school C++ and don't like these new fangled template 
constraints you can use specialisation as well:

struct Group {
     int i1;

     Group opBinary(string op : "+", U: int) (U x) {
         // do somehting
         return this;
     }

     Group opBinary(string op : "+", U: Group) (U rhs) {
         // do something
         return this;
     }
}


-- 
My enormous talent is exceeded only by my outrageous laziness.
http://www.ssTk.co.uk


More information about the Digitalmars-d-learn mailing list