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 reply "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
next sibling parent reply 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
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
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 reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 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
parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
On Monday, 5 May 2014 at 23:28:58 UTC, Nordlöw wrote:
 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.
enum IsA(A, alias B) = is(A == B!T, T);
May 05 2014
prev sibling next sibling parent reply 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
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 parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent Andrej Mitrovic via Digitalmars-d-learn writes:
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