www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [GSoC] Pass-by-value opAssign called with a ref doesn't call

--bcaec520ef993ef1f504a6648a60
Content-Type: text/plain; charset=ISO-8859-1

------------------------------
import std.stdio;

struct Test {
this( this )              { writeln( "Test.this(this)" ); }
 ~this()                   { writeln( "Test.~this()" );    }
void opAssign( Test rhs ) { writeln( "Test.opAssign" );   }
}

ref Test makeRef( ref Test test ) { return test; }

int main(string[] argv) {
Test x,y;
 writeln( "Before assignment" );
x = makeRef(y);
 writeln( "After assignment" );
 readln();
 return 0;
}


------------------------------
Output:

Before assignment
Test.opAssign
Test.~this()
After assignment
Test.~this()
Test.~this()
------------------------------

The code above assigns a reference to a Test object to another Test object.
The assignment  doesn't call the postblit constructor, yet it calls the
destructor (lines 2,3 in the output). Changing the assignment to "x = y;"
makes it call the postblit.
Is this correct behaviour, or should I file it as a bug?

---
Cristi Cobzarenco
BSc in Artificial Intelligence and Computer Science
University of Edinburgh
Profile: http://www.google.com/profiles/cristi.cobzarenco

--bcaec520ef993ef1f504a6648a60
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>------------------------------</div><div><div>import std.stdio;</div><=
div><br></div><div>struct Test {</div><div><span class=3D"Apple-tab-span" s=
tyle=3D"white-space:pre">	</span>this( this ) =A0 =A0 =A0 =A0 =A0 =A0 =A0{ =
writeln( &quot;Test.this(this)&quot; ); }</div>

<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>~this=
() =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 { writeln( &quot;Test.~this()&quot; =
); =A0 =A0}</div><div><span class=3D"Apple-tab-span" style=3D"white-space:p=
re">	</span>void opAssign( Test rhs ) { writeln( &quot;Test.opAssign&quot; =
); =A0 }</div>

<div>}</div><div><br></div><div>ref Test makeRef( ref Test test ) { return =
test; }</div><div><br></div><div>int main(string[] argv) {</div><div><span =
class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>Test x,y;</div>
<div>
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span></div><div=
<span class=3D"Apple-tab-span" style=3D"white-space:pre">	</span>writeln( =

tyle=3D"white-space:pre"> </span>x =3D makeRef(y);</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>write= ln( &quot;After assignment&quot; );</div><div><span class=3D"Apple-tab-span= " style=3D"white-space:pre"> </span></div><div><span class=3D"Apple-tab-spa= n" style=3D"white-space:pre"> </span>readln();</div> <div><span class=3D"Apple-tab-span" style=3D"white-space:pre"> </span>retur= n 0;</div><div>}</div></div><div><br></div><div><br></div><div>------------= ------------------</div><div>Output:</div><div><br></div><div>Before assign= ment</div> <div>Test.opAssign</div><div>Test.~this()</div><div>After assignment</div><= div>Test.~this()</div><div>Test.~this()</div><div>-------------------------= -----</div><div><br></div><div>The code above assigns a reference to a Test= object to another Test object. The assignment =A0doesn&#39;t call the post= blit constructor, yet it calls the destructor (lines 2,3 in the output). Ch= anging the assignment to &quot;x =3D y;&quot; makes it call the postblit.</= div> <div>Is this correct behaviour, or should I file it as a bug?</div><div><br=
</div><div>---</div>Cristi Cobzarenco<div>BSc in Artificial Intelligence a=

=3D"http://www.google.com/profiles/cristi.cobzarenco" target=3D"_blank">htt= p://www.google.com/profiles/cristi.cobzarenco</a></div> <br> --bcaec520ef993ef1f504a6648a60--
Jun 23 2011