Call destructor directly.

Ali Çehreli acehreli at yahoo.com
Mon Oct 21 00:31:27 PDT 2013


On 10/20/2013 10:59 PM, Agustin wrote:

 > That didn't work, but after reading how emplace works, i had to make
 > some changes.
 >
 >      public T allocate(T : Object, A...)(auto ref A arguments) {
 >          auto pMemory = rawAllocate(__traits(classInstanceSize, T),
 > T.alignof);

Does rawAllocate still return void*? For classes, emplace requires (or 
at least "also accepts" a slice).

Your code will be simpler if rawAllocate returned a slice:

import std.conv;

void[] rawAllocate(size_t, size_t)
{
     static ubyte[1000] buffer;
     return buffer;
}

T allocate(T : Object, A...)(auto ref A arguments) {
     auto pMemory = rawAllocate(__traits(classInstanceSize, T), T.alignof);

     return emplace!T(pMemory, arguments);
}

class C
{
     this(int i, double d)
     {}
}

void main()
{
     auto c = allocate!C(42, 1.5);
}

If it has to return void*, you can make a slice from a plain pointer 
with the following syntax:

void * rawAllocate(size_t, size_t)
{
     static ubyte[1000] buffer;
     return buffer.ptr;
}

import std.stdio;

void main()
{
     enum requiredLength = 42;
     auto rawPtr = rawAllocate(requiredLength, 16);
     auto slice = rawPtr[0..requiredLength];  // <-- slice from plain 
pointer
     writeln(slice);
}

Ali



More information about the Digitalmars-d-learn mailing list