Better handling of noncopyable objects and objects with this(this)

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 1 07:20:40 PDT 2015


On Monday, 1 June 2015 at 12:50:28 UTC, Marc Schütz wrote:
> Also, object with destructors need to have more restrictions:
>
>     S {
>         ~this();
>     }
>
>     void foo() {
>         S s;
>         if(condition)
>             bar(s);
>         // <- should we run the destructor here?
>     }
>
> This can either be solved by making such cases non-eligible, or 
> by "remembering" whether an object was moved using a hidden 
> boolean variable. AFAIK the latter is incidentally the solution 
> Rust chose.

Wouldn't the compiler just do something like

if(condition)
{
     bar(s); // Do a move; the destructor is run inside bar
     return;
}
else
{
     s.__dtor();
     return;
}

In which case, the destructor is always run in the right spot, 
and the compiler can guarantee that a move is done rather than a 
copy?

- Jonathan M Davis


More information about the Digitalmars-d mailing list