emplace, scope, enforce [Was: Re: Manual...]

bearophile bearophileHUGS at lycos.com
Wed Jul 21 03:20:38 PDT 2010


Dmitry Olshansky:
> Well, I'm using this for structs, very straightforward:
> 
> T* create(T, Args...)(Args args)
> if ( !is(T == class) ){
>      return emplace!T(malloc(T.sizeof)[0..T.sizeof], args);
> }

That's not good enough, you are allocating on the (C) heap. If you use that in the D benchmark I have shown you probably can get bad timing results.


> This is dtor not get called, and it's because emplace is a library 
> replacement for placement new( no pun).
> Sure enough with manual memory management you need to call clear(t) at exit.

If the class is allocated on the stack it's much better if the destructor is called when the class gets out of scope. Otherwise it's like C programming.
(I suggest to edit your post, to remove useless parts of the original post.)

Bye,
bearophile


More information about the Digitalmars-d mailing list