Function dynCast
Dynamic cast for shared pointers if ElementType
is class with D linkage.
{null} dynCast(T, Ptr)()
if (isSharedPtr!Ptr && !is(Ptr == shared) && !Ptr .isWeak && isClassOrInterface!T && (__traits(getLinkage, T) == "D") && isClassOrInterface!(Ptr .ElementType) && (__traits(getLinkage, Ptr .ElementType) == "D"));
Creates a new instance of SharedPtr
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 SharedPtr
is null.
Otherwise, the new SharedPtr
will share ownership with the initial value of ptr
.
Example
static class Foo{
int i;
this(int i)pure nothrow @safe @nogc{
this .i = i;
}
}
static class Bar : Foo{
double d;
this(int i, double d)pure nothrow @safe @nogc{
super(i);
this .d = d;
}
}
static class Zee{
}
{
SharedPtr!(const Foo) foo = SharedPtr!Bar .make(42, 3.14);
assert(foo .get .i == 42);
auto bar = dynCast!Bar(foo);
assert(bar != null);
assert(bar .get .d == 3.14);
static assert(is(typeof(bar) == SharedPtr!(const Bar)));
auto zee = dynCast!Zee(foo);
assert(zee == null);
static assert(is(typeof(zee) == SharedPtr!(const Zee)));
}