Operator overloading problem

Philippe Sigaud philippe.sigaud at gmail.com
Fri Aug 6 14:16:17 PDT 2010


On Fri, Aug 6, 2010 at 22:37, div0 <div0 at sourceforge.net> wrote:

> Personally, I'm with you and I would expect that the compiler should
> example the function parameters after the template string parameter but it
> doesn't.
>

Yes :o(
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;
>    }
> }
>
>
In some cases, you might factor things a bit:

Group opBinary(string op, U)(U u) if (op == "+")
{
   common code for all U's;

    static if (some test on U)
      some code;
   else
     other code;
}

Maybe  some code is common between the Group case and the int case. I'm not
sure it's more readable this way...

Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100806/26e481ef/attachment.html>


More information about the Digitalmars-d-learn mailing list