Thread overview
opAssign() calls members' postblits?
Jun 21, 2014
Marc Schütz
Jun 21, 2014
Ali Çehreli
Jun 22, 2014
Marc Schütz
June 21, 2014
    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)
June 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

June 22, 2014
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.