Thread overview | |||||
---|---|---|---|---|---|
|
November 24, 2012 passing struct literals by reference (dmd 2.060, Windows) | ||||
---|---|---|---|---|
| ||||
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. |
November 24, 2012 Re: passing struct literals by reference (dmd 2.060, Windows) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | 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
|
November 24, 2012 Re: passing struct literals by reference (dmd 2.060, Windows) | ||||
---|---|---|---|---|
| ||||
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 |
Copyright © 1999-2021 by the D Language Foundation