Presence of struct destructor makes lvalue?

Ali Çehreli acehreli at yahoo.com
Mon Apr 8 12:03:42 PDT 2013


The following program has two assignments from two rvalues:

import std.stdio;
import std.string;

// version = destructor_defined;

struct S
{
     int i;

     string info() const
     {
         return format("%s(%s)", &i, i);
     }

     void opAssign(S rhs)
     {
         writefln("%s from Rvalue %s", this.info(), rhs.info());
     }

     void opAssign(ref S rhs)
     {
         writefln("%s from Lvalue %s", this.info(), rhs.info());
     }

     version (destructor_defined)
     {
         ~this()
         {
             writefln("destroying %s", this.info());
         }
     }
}

S foo(int i)
{
     return S(i);
}

void main()
{
     auto s = S(1);

     // Assignment from two kinds of rvalues
     s = foo(2);
     s = S(3);
}

The output indicates that the two assignments go to the "rvalue 
assignment operator":

7FFF36AA0B20(1) from Rvalue 7FFF36AA0B24(2)
7FFF36AA0B20(1) from Rvalue 7FFF36AA0B28(3)

However, when S.~this() is defined (by e.g. uncommenting the 'version =' 
line), the second assignment involves an lvalue:

7FFF9571E7F8(1) from Rvalue 7FFF9571E750(2)
destroying 7FFF9571E750(2)
7FFF9571E7F8(1) from Lvalue 7FFF9571E804(3)  <-- HERE
destroying 7FFF9571E804(3)
destroying 7FFF9571E7F8(1)

I suspect this has something to do with the compiler generated 
assignment logic:

   http://dlang.org/struct.html

[quote]
Struct assignment t=s is defined to be semantically equivalent to:

t.opAssign(s);

where opAssign is a member function of S:

S* opAssign(ref const S s)
{   ... bitcopy *this into tmp ...
     ... bitcopy s into *this ...
     ... call destructor on tmp ...
     return this;
}
[/quote]

It is not clear how the presence of ~this() changes the semantics. Bug?

Thank you,
Ali


More information about the Digitalmars-d mailing list