[Issue 15842] New: struct is being copied when returned directly
    via Digitalmars-d-bugs 
    digitalmars-d-bugs at puremagic.com
       
    Mon Mar 28 06:18:31 PDT 2016
    
    
  
https://issues.dlang.org/show_bug.cgi?id=15842
          Issue ID: 15842
           Summary: struct is being copied when returned directly
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: ag0aep6g at gmail.com
                CC: atila.neves at gmail.com
This is spin-off from issue 15832.
test.d:
----
import std.stdio;
struct S
{
    this(int dummy) {writeln(&this);}
}
S f() {return S(0);}
void main()
{
    S s = f();
    writeln(&s);
}
----
The two `writeln`s print different addresses, so the struct is apparently
constructed in a different location and gets copied on return.
Change `f` to this and they print the same address:
----
S f() {S s = S(0); return s;}
----
I don't know what the ABI says about this, so this behavior may be allowed by
the spec, but it's weird how adding a temporary makes the copy go away.
--
    
    
More information about the Digitalmars-d-bugs
mailing list