digitalmars.D.learn - Is it possible to check if a type is an instance of a template?
- Andrej Mitrovic (8/8) Sep 15 2011 I can do this:
- Simen Kjaeraas (12/21) Sep 15 2011 static if can do this:
- Andrej Mitrovic (2/2) Sep 15 2011 Cool, that works, thanks. Is this in Phobos by any chance? Otherwise
- Simen Kjaeraas (13/15) Sep 15 2011 No more flexible version available, sadly. I wish this worked (I think
- Jonathan M Davis (3/4) Sep 15 2011 How could it be? It's specific to the template that you're testing.
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (10/17) May 05 2014 Can we make Foo a template parameters aswell, here?
- Rene Zwanenburg (2/19) May 05 2014 enum IsA(A, alias B) = is(A == B!T, T);
- Timon Gehr (5/13) Sep 15 2011 template bar(T : Foo!S,S){ }
- Andrej Mitrovic (2/3) Sep 15 2011 Yeah, that should do it too. Thanks.
- bearophile (6/13) May 05 2014 There is now std.traits.isInstanceOf that could do what you need.
- Andrej Mitrovic via Digitalmars-d-learn (4/5) May 06 2014 Someone resurrected a thread from 2011. Of course there's isInstanceOf
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
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 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
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
template IsAFoo(T) { static if (is(T t == Foo!U, U)) { enum IsAFoo = true; } else { enum IsAFoo = false; } }Can we make Foo a template parameters aswell, here? I tried this template IsA(T, K) { static if (is(T t == K!U, U)) { enum IsA = true; } else { enum IsA = false; } } but it doesn't quite work.
May 05 2014
On Monday, 5 May 2014 at 23:28:58 UTC, Nordlöw wrote:enum IsA(A, alias B) = is(A == B!T, T);template IsAFoo(T) { static if (is(T t == Foo!U, U)) { enum IsAFoo = true; } else { enum IsAFoo = false; } }Can we make Foo a template parameters aswell, here? I tried this template IsA(T, K) { static if (is(T t == K!U, U)) { enum IsA = true; } else { enum IsA = false; } } but it doesn't quite work.
May 05 2014
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
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
Andrej Mitrovic: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?There is now std.traits.isInstanceOf that could do what you need. Its implementation: enum bool isInstanceOf(alias S, T) = is(T == S!Args, Args...); Bye, bearophile
May 05 2014
On 5/6/14, bearophile via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:There is now std.traits.isInstanceOf that could do what you need.Someone resurrected a thread from 2011. Of course there's isInstanceOf when I added it myself at the end of 2012.
May 06 2014