Is it possible to check if a type is an instance of a template?

Simen Kjaeraas simen.kjaras at gmail.com
Thu Sep 15 13:38:30 PDT 2011


On Thu, 15 Sep 2011 22:24:50 +0200, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> I can do this:
>
> struct Foo(T) { }
> template bar(T : Foo!int) { }
>
> I can check if T is a specific instantiation of Foo. But I want to
> check whether T is *any* instantiation of Foo. Is this possible to do?
>
> Otherwise I'm currently having to hardcode via:
>
> template bar(T) if (isOneOf!(T, Foo!int, Foo!double)) { }
>
> But having to list all possible instantiations doesn't really scale too  
> well.

static if can do this:

template IsAFoo(T) {
     static if (is(T t == Foo!U, U)) {
         enum IsAFoo = true;
     } else {
         enum IsAFoo = false;
     }
}

-- 
   Simen


More information about the Digitalmars-d-learn mailing list