How is std.traits.isInstanceOf supposed to work?
TheFlyingFiddle
theflyingfiddle at gmail.com
Sun Nov 10 15:43:17 PST 2013
The docs might be a little unclear about this. The template
isInstanceOf checks to see if the second parameter is a template
instantiation of the first parameter. It does not have anything
to do with inheritance.
Like so:
struct S(T) { T t; }
struct S2(T) { T t; }
import std.traits;
unittest
{
alias inst = S!(int);
alias inst2 = S2!(string);
//S!(int) is an instantiation of S
static assert(isInstanceOf!(S, inst));
//S2!(string) is not an instantiation of S
static assert(!isInstanceOf!(S, inst2));
}
If you would like to see if something is derived from something
or to see if a struct is convertible to another struct use the is
keyword.
Like so:
struct S3
{
S!(int) s;
alias s this;
}
class C { }
class D : C { }
unittest
{
//S3 is convertible to S!(int)
static assert(is(S3 : S!(int)));
//D is convertible (derived from) C
static assert(is(D : C));
}
More information about the Digitalmars-d
mailing list