opEquals(const ref yadaYada)

lws invalid at email.com
Mon Dec 14 01:06:31 PST 2009


I don't know if I believe this is necesarrily bad.  It's revealing some 
bad coding on your part.

You shouldn't be doing opEquals with an rvalue of a class.   Make 
getFoo return a reference.

ref Foo getFoo() {} fixes the problem and avoids value-copying for no 
reason to an rvalue that's going to get garbage collected.

-lws


On 2009-12-12 07:14:50 -0800, dsimcha <dsimcha at yahoo.com> said:

> I've noticed that, for DMD 2.037, we've started mandating that the input
> parameter for struct opEquals be const ref T.  This seemed like a good idea
> initially, but it creates the horribly leaky abstraction that the right-hand
> argument to opEquals can't be an rvalue.  Example:
> 
> struct Foo {
>     bool opEquals(const ref Foo rhs) const {  // Only signature
>                                               // that compiles.
>         return true;
>     }
> }
> 
> Foo getFoo() {
>     return Foo();
> }
> 
> void main() {
>     Foo foo = getFoo();
>     bool isEqual = foo == getFoo();
> }
> 
> Error:  Foo.opEquals type signature should be const bool(ref const(Foo)) not
> const bool(Foo rhs)
> 
> Will this be getting fixed witht he new operator overloading?





More information about the Digitalmars-d mailing list