import btl.autoptr.shared_ptr; import btl.autoptr.rc_ptr; import btl.autoptr.intrusive_ptr; import core.lifetime : move; static class Foo{ ControlBlock!(int, int) c; int i; this(int i)pure nothrow @safe @nogc{ this.i = i; } ~this()pure nothrow @safe @nogc{ i = -1; } } ()@safe{ auto a = SharedPtr!long.make(42); auto b = RcPtr!float.make(3.14); auto c = IntrusivePtr!Foo.make(123); assert(a.useCount == 1); assert(b.useCount == 1); assert(c.useCount == 1); int i = apply!((scope long* x, scope float* y, scope Foo z){ assert(a.useCount == 2); assert(b.useCount == 2); a = null; b = null; c = null; assert(z.i != -1); //x, y, z are still valid until end of the scope. return z.i; })(a, b, move(c)); assert(i == 123); }();