On 06/02/2010 03:34 PM, bearophile wrote:
Andrei Alexandrescu:
To ensure the destructor is called, I guess we need this:

@system struct StackAllocated(T)
{
      private ubyte buffer[__traits(classInstanceSize, T)] = void;
      @property T payload() { return cast(T) buffer.ptr; }
      alias payload this;
      @disable this(); // meh
      @disable this(this);
      this(Args...)(auto ref Args args)
      {
          emplace!T(buffer.ptr, buffer.length);
      }
      ~this()
      {
          clear(payload);
      }
}

Is this useful?

static if (__traits(hasMember, T, "__dtor")) {
      ~this() {
          clear(payload);
      }
}

Bye,
bearophile

Absolutely!

Andrei

Reply via email to