Delay allocating class instance in stack.

ANtlord via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 21 21:57:35 PDT 2017


On Tuesday, 21 March 2017 at 08:46:43 UTC, Ali Çehreli wrote:
> Another option is std.conv.emplace:
>
> import std.conv : emplace;
>
> class MyClass {
>     this(int) @nogc {
>     }
>
>     ~this() @nogc {
>     }
> }
>
> void method(bool flag) @nogc
> {
>     void[__traits(classInstanceSize, MyClass)] buffer = void;
>     MyClass obj;
>
>     if(flag) {
>         obj = emplace!MyClass(buffer, 1);
>     } else {
>         obj = emplace!MyClass(buffer, 2);
>     }
>
>     // Unfortunately, destroy() is not @nogc
>     // scope(exit) destroy(obj);

Thank you for clarification. But I have one more question. Do I 
have to use destroy for deallocating object from stack?



More information about the Digitalmars-d-learn mailing list