On Monday, 5 October 2015 at 22:15:59 UTC, bitwise wrote:
On Monday, 5 October 2015 at 21:29:20 UTC, Namespace wrote:
But you can simply relinquish alias this and use opDispatch. Problem solved.

I don't understand what you mean.

----
import std.stdio;

struct Scoped(T) {
    private void[__traits(classInstanceSize, T)] buf = void;

    this(Args...)(auto ref Args args) {
        this.buf = typeid(T).init[];
        this.get().__ctor(args);
    }

    ~this() {
        .destroy(this.get());
    }


    private T get() {
        return cast(T) this.buf.ptr;
    }

    auto opDispatch(string method, Args...)(auto ref Args args) {
        return mixin("this.get()." ~ method ~ "(args)");
    }
}

auto scoped(T, Args...)(auto ref Args args) {
    return Scoped!T(args);
}

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 a0 = scoped!A("Test"); <-- fails

    auto a1 = scoped!A("Foo");
    a1.hello();

    auto a2 = scoped!A("Bar");
    a2.hello();
}
----

Reply via email to