Why do struct literals count as lvalues?

Jonathan M Davis jmdavisProg at gmx.com
Thu Aug 18 13:19:13 PDT 2011


On Thursday, August 18, 2011 12:49 kennytm wrote:
> Trass3r <un at known.com> wrote:
> > struct A {}
> > static A bar()
> > {
> > 
> > return A();
> > 
> > }
> > void foo(ref A a) {}
> > void main()
> > {
> > 
> > foo(A()); // works
> > foo(bar()); // doesn't
> > 
> > }
> > 
> > Where's the difference?
> 
> The difference -- you've answered yourself in the title ;).
> 
> Reason why struct literals are lvalues -- because Walter and others believe
> this is valid. Check the discussion in bugzilla issues 5178 and 5889.

Yeah. I don't understand why a struct literal would be an lvalue. It's a 
temporary. What possible value does it have? It's not a variable. It doesn't 
refer to a variable. Why would you be able to assign to anything which is not 
a variable (or indirectly refers to one - e.g. with ref)? The result is 
incredibly confusing - especially when you mix const ref into the mix. I grant 
you that changing it now would break code - especially given 
http://d.puremagic.com/issues/show_bug.cgi?id=3659 - but it serves no real 
purpose except to confuse as far as I can see.

- Jonathan M Davis


More information about the Digitalmars-d mailing list