Problem with dtor behavior

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 28 09:25:01 PDT 2017


On Thursday, 27 July 2017 at 19:19:27 UTC, SrMordred wrote:
> void push(T)(auto ref T value){
>     push(MyStruct(1));

> template<class T>
> void push(T&& value){
>     push(MyStruct(1));


Those aren't the same... the D one will pass by value, the C++ 
one won't. D's auto ref means ref for lvalues, value for rvalues. 
C++ will do rvalue by ref too.

> I didnt expected to see two dtors in D (this destroy any 
> attempt to free resources properly on the destructor).

In D, you must write destructors such that they can be called on 
a default-initialized object. (This also means your malloc is 
wrong, since it doesn't perform the default initialization.)

> Can someone explain why is this happening and how to achieve 
> the same behavior as c++?

I would blit it over as raw memory then call the ctor instead of 
using the assign operator. That's what std.conv.emplace does..




More information about the Digitalmars-d-learn mailing list