<div dir="ltr">2013/11/4 <<a href="mailto:luis@luismarques.eu">luis@luismarques.eu</a>>"@<a href="http://puremagic.com">puremagic.com</a> <span dir="ltr"><<a href="mailto:"\"Luís".Marques"" target="_blank">"\"Luís".Marques"</a>></span><br>
<div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi,<br>
<br>
Most of the time my D code has been high-level, so I had never considered the following issue. Imagine you have a struct A as a member of a class/struct X (here a struct, to ensure the dtor is called):<br>
<br>
    struct A<br>
    {<br>
        int v;<br>
<br>
        this(int v)<br>
        {<br>
            this.v = v*2;<br>
        }<br>
<br>
        ~this()<br>
        {<br>
            writefln("~A(%d)", v);<br>
        }<br>
    }<br>
<br>
    struct X<br>
    {<br>
        A a;<br>
<br>
        this(int v)<br>
        {<br>
            a = A(v);<br>
            writeln("-");<br>
        }<br>
    }<br>
<br>
    void main()<br>
    {<br>
        X x = X(42);<br>
    }<br>
<br>
Output:<br>
<br>
    ~A(0)<br>
    -<br>
    ~A(84)<br>
<br>
That is, because we don't have C++'s colon initialization syntax, we are paying the cost of initializing (and then destroying) X.a before we assign to it with "a = A(v)" in X's ctor. This seems to be the case even with @disable A.this(), which here does not seem to do anything (does not prevent the default/implicit initialization of X.a, before it is assigned A(v) ).<br>

<br>
If C++ distinguishes between initialization and assignment to avoid this issue, is there a reason why D can avoid making the distinction? That is a performance issue. How about correctness? For instance:<br>
<br>
    struct A<br>
    {<br>
        void* mem;<br>
<br>
        @disable this();<br>
<br>
        this(int v)<br>
        {<br>
            mem = malloc(v);<br>
        }<br>
<br>
        ~this()<br>
        {<br>
            free(mem);<br>
        }<br>
    }<br>
<br>
Now we can't have an A as a member of X? (it would free a null pointer)<br>
<br>
How have you solved these cases? Do you change it to a PIMPL? What if that's not desirable? What if you don't want to break encapsulation / cleanliness too much? Etc. Is there a good general solution for this issue?<br>

</blockquote></div><br></div><div class="gmail_extra">The issue is timely fixed in 2.064.</div><div class="gmail_extra"><a href="http://d.puremagic.com/issues/show_bug.cgi?id=9665">http://d.puremagic.com/issues/show_bug.cgi?id=9665</a><br>
</div><div class="gmail_extra"><a href="https://github.com/D-Programming-Language/dlang.org/pull/404">https://github.com/D-Programming-Language/dlang.org/pull/404</a><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">
Therefore with 2.064, the first test case will output following:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_extra">    -</div><div class="gmail_extra">    ~A(84)</div><div><br></div>
<div><br></div><div>Kenji Hara<br></div></div></div>