isInstanceOf

Returns true if T is an instance of the template S.

  1. enum bool isInstanceOf(alias S, T);
  2. template isInstanceOf(alias S, alias T)
    template isInstanceOf () {
    enum isInstanceOf;
    }

Examples

1 static struct Foo(T...) { }
2 static struct Bar(T...) { }
3 static struct Doo(T) { }
4 static struct ABC(int x) { }
5 static void fun(T)() { }
6 template templ(T) { }
7 
8 static assert(isInstanceOf!(Foo, Foo!int));
9 static assert(!isInstanceOf!(Foo, Bar!int));
10 static assert(!isInstanceOf!(Foo, int));
11 static assert(isInstanceOf!(Doo, Doo!int));
12 static assert(isInstanceOf!(ABC, ABC!1));
13 static assert(!isInstanceOf!(Foo, Foo));
14 static assert(isInstanceOf!(fun, fun!int));
15 static assert(isInstanceOf!(templ, templ!int));

To use isInstanceOf to check the identity of a template while inside of said template, use TemplateOf.

1 static struct A(T = void)
2 {
3     // doesn't work as expected, only accepts A when T = void
4     void func(B)(B b) if (isInstanceOf!(A, B)) {}
5 
6     // correct behavior
7     void method(B)(B b) if (isInstanceOf!(TemplateOf!(A), B)) {}
8 }
9 
10 A!(void) a1;
11 A!(void) a2;
12 A!(int) a3;
13 
14 static assert(!__traits(compiles, a1.func(a3)));
15 static assert( __traits(compiles, a1.method(a2)));
16 static assert( __traits(compiles, a1.method(a3)));

Meta

Suggestion Box / Bug Report