UFCS and operator overloading

Jens Mueller jens.k.mueller at gmx.de
Mon May 7 14:06:23 PDT 2012


Nick Sabalausky wrote:
> "Jens Mueller" <jens.k.mueller at gmx.de> wrote in message 
> news:mailman.391.1336410464.24740.digitalmars-d at puremagic.com...
> > Hi,
> >
> > from my understanding UFCS is supposed to work with operator overloading. 
> > I.e.
> > in the following a + b should work
> >
> > struct Foo {}
> >
> > Foo opBinary(string op)(Foo lhs, Foo rhs) if (op == "+")
> > {
> >    return Foo.init;
> > }
> >
> > unittest
> > {
> >    Foo a, b;
> >    a + b; // fails to compile
> > }
> >
> > Is UFCS supposed to work with operator overloading, isn't it?
> >
> > Jens
> 
> I don't know why that doesn't work (unless you just need to make it "auto c 
> = a + b;" so it isn't a "statement has no effect"?),

auto c = ... has no effect. I'm just ignoring the return value.

> but FWIW that's not an example of UFCS. UFCS would mean calling your
> opBinary above like this:
> 
> a.opBinary!"+"(b)
> 
> Instead of this:
> 
> opBinary!"+"(a, b)

Right. So what is a + b rewritten to. I assume it is rewritten to
a.opBinary!"+"(b). And then UFCS should kick in and rewrite it to
opBinary!("+")(a, b).

You seem to believe that a + b is rewritten to opBinary!("+")(a, b).
Right?

Jens


More information about the Digitalmars-d mailing list