Ok a follow-up to my previous email, it has nothing to do with opAssign in particular. Any function that takes an argument by value, pass it the result of a function that returns a reference and it won't call the postblit. Calling it with a ref argument on the other hand works properly:<div>
<br></div><div><div>import std.stdio;</div><div><br></div><div>struct Test {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>this( this ) { writeln( "Test.this(this)" ); }</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>~this() { writeln( "Test.~this()" ); }</div>
<div>}</div><div><br></div><div>ref Test returnsRef( ref Test x ) { return x; }</div><div>void takesVal( Test x ) {}</div><div>void takesRef( ref Test x ) {</div><div> takesVal( x );</div><div>}</div><div><br></div><div>
int main() {</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>Test x;</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>writeln( "-- with ref argument (works)" );</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>takesRef( x );</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>writeln( "-- with ref return (doesn't work)" );</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>takesVal( returnsRef( x ) );</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>writeln( "-- done");</div>
<div><span class="Apple-tab-span" style="white-space:pre"> </span>return 0;</div><div>}</div><div><br></div><div>I'm starting to be pretty sure this is a bug.</div><div>---</div>Cristi Cobzarenco<div>BSc in Artificial Intelligence and Computer Science</div>
<div>University of Edinburgh<br>Profile: <a href="http://www.google.com/profiles/cristi.cobzarenco" target="_blank">http://www.google.com/profiles/cristi.cobzarenco</a></div><br>
<br><br><div class="gmail_quote">On 23 June 2011 19:37, Cristi Cobzarenco <span dir="ltr"><<a href="mailto:cristi.cobzarenco@gmail.com">cristi.cobzarenco@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>------------------------------</div><div><div>import std.stdio;</div><div><br></div><div>struct Test {</div><div><span style="white-space:pre-wrap"> </span>this( this ) { writeln( "Test.this(this)" ); }</div>
<div><span style="white-space:pre-wrap"> </span>~this() { writeln( "Test.~this()" ); }</div><div><span style="white-space:pre-wrap"> </span>void opAssign( Test rhs ) { writeln( "Test.opAssign" ); }</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 style="white-space:pre-wrap"> </span>Test x,y;</div><div>
<span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>writeln( "Before assignment" );</div><div><span style="white-space:pre-wrap"> </span>x = makeRef(y);</div>
<div><span style="white-space:pre-wrap"> </span>writeln( "After assignment" );</div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>readln();</div>
<div><span style="white-space:pre-wrap"> </span>return 0;</div><div>}</div></div><div><br></div><div><br></div><div>------------------------------</div><div>Output:</div><div><br></div><div>Before assignment</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 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.</div>
<div>Is this correct behaviour, or should I file it as a bug?</div><div><br></div><div>---</div>Cristi Cobzarenco<font color="#888888"><div>BSc in Artificial Intelligence and Computer Science</div><div>University of Edinburgh<br>
Profile: <a href="http://www.google.com/profiles/cristi.cobzarenco" target="_blank">http://www.google.com/profiles/cristi.cobzarenco</a></div>
<br>
</font></blockquote></div><br></div>