operator overloading compilation error
Diego Canuhé
canuhedc at gmail.com
Tue Jul 26 05:42:15 PDT 2011
thanks!
On Tue, Jul 26, 2011 at 9:12 AM, bearophile <bearophileHUGS at lycos.com>wrote:
> Diego Canuhé:
>
> > I'm trying to overload the "+" and "-" operators for a struct but I get
> > this error
> >
> > test.d(47): Error: incompatible types for ((v1) - (v2)): 'Vecf!(1u)' and
> > 'Vecf!(1u)'
> > (the line corresponds to the last assert)
>
> It's a dmd bug, and I think it's already in Bugzilla. This is workaround
> code:
>
>
> struct Vecf(uint n) {
> float x;
> Vecf opBinary(string op)(Vecf other) if (op == "+" || op == "-") {
> mixin("return Vecf(this.x " ~ op ~ " other.x);");
> }
> }
>
> void main() {
> alias Vecf!1 V;
> auto v1 = V(3f);
> auto v2 = V(5f);
> auto r = V(-2f);
> assert ((v1.opBinary!"-"(v2)) == r); // better to use an approximate ==
> assert ((v1 - v2) == r); // better to use an approximate ==
> }
>
> Bye,
> bearophile
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20110726/e678a7da/attachment.html>
More information about the Digitalmars-d-learn
mailing list