www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it possible to check if a type is an instance of a template?

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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.
Sep 15 2011
next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 15 Sep 2011 22:24:50 +0200, Andrej Mitrovic  
<andrej.mitrovich 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
Sep 15 2011
prev sibling next sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/15/2011 10:24 PM, 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.

template bar(T : Foo!S,S){ } S will be bound to the type that was used to instantiate Foo. It won't work if Foo takes a variable number of parameters though. Probably a bug.
Sep 15 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Cool, that works, thanks. Is this in Phobos by any chance? Otherwise
I'll just use a more flexible version of that.
Sep 15 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 9/15/11, Timon Gehr <timon.gehr gmx.ch> wrote:
 template bar(T : Foo!S,S){ }

Yeah, that should do it too. Thanks.
Sep 15 2011
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Thursday, September 15, 2011 13:45 Andrej Mitrovic wrote:
 Cool, that works, thanks. Is this in Phobos by any chance?

How could it be? It's specific to the template that you're testing. - Jonathan M Davis
Sep 15 2011
prev sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Thu, 15 Sep 2011 22:45:21 +0200, Andrej Mitrovic  
<andrej.mitrovich gmail.com> wrote:

 Cool, that works, thanks. Is this in Phobos by any chance? Otherwise
 I'll just use a more flexible version of that.

No more flexible version available, sadly. I wish this worked (I think it's in Bugzilla somewhere): template IsA( alias F, T ) { static if ( is( T t == F!U, U ) ) { enum IsA = true; } else { enum IsA = false; } } -- Simen
Sep 15 2011