It's a step simpler with the new inline feature (works sadly only with the -inline flag):

----
pragma(inline, true)
auto scoped(T, Args...)(auto ref Args args) if (is(T == class)) {
    void[__traits(classInstanceSize, T)] buf = void;
    buf[] = typeid(T).init[];

    T obj = cast(T) buf.ptr;
    obj.__ctor(args);

    return obj;
}

class A {
    string name;

    this(string name) {
       this.name = name;
       writeln("Creating A");
    }

    ~this() {
       writeln("Destroying A");
    }

    void hello() {
       writeln("Hello, ", this.name);
    }
}

void main() {
    A a1 = scoped!A("Foo");
    a1.hello();
}
----

Reply via email to