www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 15842] New: struct is being copied when returned directly


          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 puremagic.com
          Reporter: ag0aep6g gmail.com
                CC: atila.neves gmail.com

This is spin-off from issue 15832.


import std.stdio;

struct S
    this(int dummy) {writeln(&this);}

S f() {return S(0);}

void main()
    S s = f();

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.

Mar 28 2016