www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - passing struct literals by reference (dmd 2.060, Windows)

reply "Jack Applegame" <japplegame gmail.com> writes:
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.
Nov 24 2012
next sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Nov 24 2012
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, November 24, 2012 02:11:23 Jonathan M Davis wrote:
 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.
http://d.puremagic.com/issues/show_bug.cgi?id=9069 - Jonathan M Davis
Nov 24 2012