WTF did happen with struct constructor and ref in 2.061 ?

deadalnix deadalnix at gmail.com
Fri Jan 4 11:35:51 PST 2013


On Friday, 4 January 2013 at 19:15:03 UTC, Ali Çehreli wrote:
> > This may not have a storage :
> > foo(funcThatReturnsS());
>
> I don't see that. funcThatReturnsS returns an S, which must 
> have a storage as well.
>

This is where things are subtle. Depending on the calling 
convention, the struct may be returned into a register. In such 
case it has no storage in memory.

> I agree with all of that. (Although, the French grammar rule of 
> that space before the question mark is hurting my eyes! :p)
>

In fact, this is even worse, french grammar specify that this 
should be a non-breaking space :D

> > Code above show there is.
>
> I don't see that. There is storage for the rvalue returned from 
> funcThatReturnsBar() as well, no?
>
> struct Bar {
>     uint i;
>
>     this(uint foo) {
>         import std.stdio;
>         writeln(&this);
>     }
> }
>
> void foo(Bar bar)
> {}
>
> Bar funcThatReturnsBar()
> {
>     return Bar(1);
> }
>
> void main() {
>     foo(Bar(0));
>     foo(funcThatReturnsBar());
> }
>
> That program prints two addresses for me:
>
> 7FFFF1076290
> 7FFFF1076298
>
> Ali

You have to understand that Bar(1) within funcThatReturnsBar have 
a storage, but it is then moved when returned. It can be moved to 
a register for instance, so I can't take a reference.

In the example terms, 7FFFF1076298 is the location on stack of 
Bar(1). But this location don't exist anymore when you return. On 
x86, The value will be in EAX in this particular example, so it 
effectively cannot be referenced.


More information about the Digitalmars-d mailing list