1 static class Foo{ 2 int i; 3 4 this(int i)pure nothrow @safe @nogc{ 5 this.i = i; 6 } 7 } 8 9 static class Bar : Foo{ 10 double d; 11 12 this(int i, double d)pure nothrow @safe @nogc{ 13 super(i); 14 this.d = d; 15 } 16 } 17 18 static class Zee{ 19 } 20 21 { 22 RcPtr!(const Foo) foo = RcPtr!Bar.make(42, 3.14); 23 assert(foo.get.i == 42); 24 25 auto bar = dynCast!Bar(foo); 26 assert(bar != null); 27 assert(bar.get.d == 3.14); 28 static assert(is(typeof(bar) == RcPtr!(const Bar))); 29 30 auto zee = dynCast!Zee(foo); 31 assert(zee == null); 32 static assert(is(typeof(zee) == RcPtr!(const Zee))); 33 } 34 35 { 36 RcPtr!(const Foo) foo = RcPtr!Bar.make(42, 3.14); 37 assert(foo.get.i == 42); 38 39 import core.lifetime : move; 40 auto bar = dynCast!Bar(foo.move); 41 assert(bar != null); 42 assert(bar.get.d == 3.14); 43 static assert(is(typeof(bar) == RcPtr!(const Bar))); 44 } 45 46 { 47 RcPtr!(const Foo) foo = RcPtr!Bar.make(42, 3.14); 48 assert(foo.get.i == 42); 49 50 auto bar = dynCastMove!Bar(foo); 51 assert(foo == null); 52 assert(bar != null); 53 assert(bar.get.d == 3.14); 54 static assert(is(typeof(bar) == RcPtr!(const Bar))); 55 }
Dynamic cast for shared pointers if ElementType is class with D linkage.
Creates a new instance of RcPtr whose stored pointer is obtained from ptr's stored pointer using a dynaic cast expression.
If ptr is null or dynamic cast fail then result RcPtr is null. Otherwise, the new RcPtr will share ownership with the initial value of ptr.