www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opAssign() calls members' postblits?

reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
     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
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Saturday, 21 June 2014 at 20:03:26 UTC, Ali Çehreli wrote:
 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.
Thank you, this makes sense now.
Jun 22 2014