View source code
Display the source code in std/traits.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Enum member std.traits.isInstanceOf

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

enum isInstanceOf(alias S, T) = is(T == S!Args, Args...);
enum isInstanceOf(alias S, alias T) = impl!T;


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)));


Walter Bright, Tomasz Stachowiak (isExpressions), Andrei Alexandrescu, Shin Fujishiro, Robert Clipsham, David Nadlinger, Kenji Hara, Shoichi Kato


Boost License 1.0.