Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 15, 2011 Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
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. |
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 |
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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.
|
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | Cool, that works, thanks. Is this in Phobos by any chance? Otherwise I'll just use a more flexible version of that. |
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On 9/15/11, Timon Gehr <timon.gehr@gmx.ch> wrote:
> template bar(T : Foo!S,S){ }
Yeah, that should do it too. Thanks.
|
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
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
|
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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 |
May 05, 2014 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | > 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 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nordlöw | 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 06, 2014 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
|
Copyright © 1999-2021 by the D Language Foundation