[dmd-beta] D 2.059 beta 4

Jonathan M Davis jmdavisProg at gmx.com
Mon Apr 9 19:43:01 PDT 2012


On Monday, April 09, 2012 17:24:17 Walter Bright wrote:
> On 4/9/2012 4:25 PM, Jonathan M Davis wrote:
> > All in all, this is way too messy. Bearophile's suggestion of using const
> > auto ref will probably work though as long as you template the function
> > (since auto ref doesn't currently work with non-templated functions). -
> > Jonathan M Davis
> Templating a struct opEquals doesn't work because templates can't be
> installed in the TypeInfo.
> 
> I'm beginning to think that disallowing struct literals to be lvalues was a
> bad idea.

The issue isn't that struct literals are now rvalues. The problem exists 
regardless of that. If all you have is

bool opEquals(const ref S rhs) {...}
S foo() {...}

then

auto s = S(1);
assert(s == s); //compiles
assert(s == foo()); //fails to compile

If you make struct literals lvalues, then this will compile

assert(s == S(1));

and if they're rvalues, it won't. But the problem with rvalues remains 
regardless, and from what I recall of the discussions of struct literals, most 
everyone thought that they should be rvalues. It's incredibly inconsistent 
IMHO to have struct literals be lvalues whereas all other temporaries are 
rvalues. From what I can tell, it just causes confusion - especially when C++ 
programmers expect const ref to work with temporaries.

The core problem is the fact that const ref requires an lvalue. If it were 
like C++ and took temporaries, then we wouldn't have this problem. The 
solution that we seem to have gone with in lieu of that is to introduce auto 
ref, but as long as that only works with templated functions, it doesn't solve 
this issue.

- Jonathan M Davis


More information about the dmd-beta mailing list