digitalmars.D.learn - Is it possible to check if a type is an instance of a template?
- Andrej Mitrovic <andrej.mitrovich gmail.com> Sep 15 2011
- "Simen Kjaeraas" <simen.kjaras gmail.com> Sep 15 2011
- Timon Gehr <timon.gehr gmx.ch> Sep 15 2011
- Andrej Mitrovic <andrej.mitrovich gmail.com> Sep 15 2011
- Andrej Mitrovic <andrej.mitrovich gmail.com> Sep 15 2011
- "Jonathan M Davis" <jmdavisProg gmx.com> Sep 15 2011
- "Simen Kjaeraas" <simen.kjaras gmail.com> Sep 15 2011
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
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
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
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
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
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
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









"Simen Kjaeraas" <simen.kjaras gmail.com> 