digitalmars.D.learn - opAssign() calls members' postblits?
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (34/34) Jun 21 2014 import std.stdio;
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/40) Jun 21 2014 Note that opAssign takes by-value. The post-blits that you see are due
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (2/9) Jun 22 2014 Thank you, this makes sense now.
import std.stdio; struct A { this(this) { writeln("A.this(this)"); } B b1, b2; } struct B { this(this) { writeln("B.this(this)"); } } void main() { A a, b; a = b; } This outputs: B.this(this) B.this(this) A.this(this) Now, http://dlang.org/struct.html#AssignOverload says: ref S opAssign(S s) { S tmp = this; // bitcopy this into tmp this = s; // bitcopy s into this tmp.__dtor(); // call destructor on tmp return this; } Note the term "bitcopy", which for me implies that no postblits are supposed to be called. What the compiler does makes sense to me, but it seems to contradict the documentation. I guess the call to the postblit is missing in the pseudo-code... Am I right? (DMD git 514db2e212ae52e1206d4296e48d4a689370d04b)
Jun 21 2014
On 06/21/2014 11:56 AM, "Marc Schütz" <schuetzm gmx.net>" wrote:import std.stdio; struct A { this(this) { writeln("A.this(this)"); } B b1, b2; } struct B { this(this) { writeln("B.this(this)"); } } void main() { A a, b; a = b; } This outputs: B.this(this) B.this(this) A.this(this) Now, http://dlang.org/struct.html#AssignOverload says: ref S opAssign(S s) {Note that opAssign takes by-value. The post-blits that you see are due that copy. i.e. b in main is copied to the argument that opAssign sees. Also note that if the right-hand side in main() were an rvalue, then that copy would not involve any post-blit.S tmp = this; // bitcopy this into tmp this = s; // bitcopy s into this tmp.__dtor(); // call destructor on tmp return this; } Note the term "bitcopy", which for me implies that no postblits are supposed to be called. What the compiler does makes sense to me, but it seems to contradict the documentation. I guess the call to the postblit is missing in the pseudo-code... Am I right? (DMD git 514db2e212ae52e1206d4296e48d4a689370d04b)Ali
Jun 21 2014
On Saturday, 21 June 2014 at 20:03:26 UTC, Ali Çehreli wrote:Thank you, this makes sense now.Now, http://dlang.org/struct.html#AssignOverload says: ref S opAssign(S s) {Note that opAssign takes by-value. The post-blits that you see are due that copy. i.e. b in main is copied to the argument that opAssign sees.
Jun 22 2014