1 { 2 SharedPtr!long x = SharedPtr!long.make(123); 3 assert(x.useCount == 1); 4 5 SharedPtr!long a = x; //lvalue copy ctor 6 assert(a == x); 7 8 const SharedPtr!long b = x; //lvalue copy ctor 9 assert(b == x); 10 11 SharedPtr!(const long) c = x; //lvalue ctor 12 assert(c == x); 13 14 const SharedPtr!long d = b; //lvalue ctor 15 assert(d == x); 16 17 assert(x.useCount == 5); 18 } 19 20 { 21 import core.lifetime : move; 22 SharedPtr!long x = SharedPtr!long.make(123); 23 assert(x.useCount == 1); 24 25 SharedPtr!long a = move(x); //rvalue copy ctor 26 assert(a.useCount == 1); 27 28 const SharedPtr!long b = move(a); //rvalue copy ctor 29 assert(b.useCount == 1); 30 31 SharedPtr!(const long) c = b.load; //rvalue ctor 32 assert(c.useCount == 2); 33 34 const SharedPtr!long d = move(c); //rvalue ctor 35 assert(d.useCount == 2); 36 } 37 38 { 39 import core.lifetime : move; 40 auto u = UniquePtr!(long, SharedControlBlock).make(123); 41 42 SharedPtr!long s = move(u); //rvalue copy ctor 43 assert(s != null); 44 assert(s.useCount == 1); 45 46 SharedPtr!long s2 = UniquePtr!(long, SharedControlBlock).init; 47 assert(s2 == null); 48 } 49 50 { 51 import core.lifetime : move; 52 auto rc = RcPtr!(long).make(123); 53 assert(rc.useCount == 1); 54 55 SharedPtr!long s = rc; 56 assert(s != null); 57 assert(s.useCount == 2); 58 assert(rc.useCount == 2); 59 60 SharedPtr!long s2 = RcPtr!(long).init; 61 assert(s2 == null); 62 }
Constructs a SharedPtr which shares ownership of the object managed by rhs.
If rhs manages no object, this manages no object too. If rhs if rvalue then ownership is moved. The template overload doesn't participate in overload resolution if ElementType of typeof(rhs) is not implicitly convertible to ElementType. If rhs if WeakType then this ctor is equivalent to this(rhs.lock()).