passing struct literals by reference (dmd 2.060, Windows)

Jonathan M Davis jmdavisProg at gmx.com
Sat Nov 24 02:11:23 PST 2012


On Saturday, November 24, 2012 10:59:24 Jack Applegame wrote:
> struct Foo {
>    int a;
> }
> void modify(ref Foo foo) {
>    foo.a++;
> }
> void main() {
>    modify(Foo(1));
> }
> 
> Why compiler doesn't report error? So as struct literal "Foo()"
> isn't a lvalue, it's impossible to pass it by reference. Isn't it?
> Just like string or numeric literals.

It should be illegal, but in C++, struct literals are lvalues for some reason, 
and D inherited that stupidity (it's just less noticeable in C++, because of 
how C++ handles const&). We managed to get Walter to change it temporarily, 
but he seems to have changed it back. I thought that I'd reported it, but I 
don't see a bug report for it now. I'll have to go create one.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list