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

Jonathan M Davis jmdavisProg at gmx.com
Thu Sep 15 13:46:18 PDT 2011


On Thursday, September 15, 2011 13:24 Andrej Mitrovic 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.

Every template instantiation is a new set of code with _zero_ assocation with 
any other template instantation. Foo!int and Foo!float might as well be FooInt 
and FooFloat for how related they are. So no, there is no easy way to check 
whether a type is an instantation of a particular template.

If you want a more scalable way to test it though, then you can add something 
to Foo that you can test for. For instance, it could have a static isFoo 
function. It wouldn't have to do anything - just exist - and you might even be 
able to make it private.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list