[Dlang-study] [lifetime] destructor, difference between C++ and D and rationale

Andrei Alexandrescu andrei at erdani.com
Wed Oct 28 15:33:05 PDT 2015


On 10/28/2015 04:39 PM, deadal nix wrote:
> Andrei, to quote you :
> "[...]at least in v1.0 we're considering having the callER do the
> increment and the callEE do the decrement. This is how D currently
> handles copy construction and destruction, and it turns out it has a
> number of advantages over C++ (where the caller does all the work)."
>
> Can you explain how C++ and D destructor management differs, and what
> are the rationale behind these differences ?

Not much of the place here, but in brief in C++ consider the call:

class C { ... };
void fun(C);
int main() { C obj; fun(obj); }

In the call to fun, the callER calls the copy ctor of C, invokes fun, 
then calls the dtor of C.

In D, the callER calls the copy ctor, but it's the callEE that calls the 
dtor.


Andrei


More information about the Dlang-study mailing list