The OpenD Programming Language

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

static struct Foo(T...) { }
static struct Bar(T...) { }
static struct Doo(T) { }
static struct ABC(int x) { }
static void fun(T)() { }
template templ(T) { }

static assert(isInstanceOf!(Foo, Foo!int));
static assert(!isInstanceOf!(Foo, Bar!int));
static assert(!isInstanceOf!(Foo, int));
static assert(isInstanceOf!(Doo, Doo!int));
static assert(isInstanceOf!(ABC, ABC!1));
static assert(!isInstanceOf!(Foo, Foo));
static assert(isInstanceOf!(fun, fun!int));
static assert(isInstanceOf!(templ, templ!int));

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

static struct A(T = void)
{
    // doesn't work as expected, only accepts A when T = void
    void func(B)(B b) if (isInstanceOf!(A, B)) {}

    // correct behavior
    void method(B)(B b) if (isInstanceOf!(TemplateOf!(A), B)) {}
}

A!(void) a1;
A!(void) a2;
A!(int) a3;

static assert(!__traits(compiles, a1.func(a3)));
static assert( __traits(compiles, a1.method(a2)));
static assert( __traits(compiles, a1.method(a3)));

Meta