Thread overview | |||||
---|---|---|---|---|---|
|
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
On Thursday, September 15, 2011 13:24 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.
Every template instantiation is a new set of code with _zero_ assocation with any other template instantation. Foo!int and Foo!float might as well be FooInt and FooFloat for how related they are. So no, there is no easy way to check whether a type is an instantation of a particular template.
If you want a more scalable way to test it though, then you can add something to Foo that you can test for. For instance, it could have a static isFoo function. It wouldn't have to do anything - just exist - and you might even be able to make it private.
- Jonathan M Davis
|
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
On 9/15/11, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> Every template instantiation is a new set of code with _zero_ assocation
> with
> any other template instantation.
Yeah, I know. But I don't think this has to be set in stone. Having some specific compile-time type information about a template instance would be cool.
|
September 15, 2011 Re: Is it possible to check if a type is an instance of a template? | ||||
---|---|---|---|---|
| ||||
On Thursday, September 15, 2011 14:17 Andrej Mitrovic wrote:
> On 9/15/11, Jonathan M Davis <jmdavisProg@gmx.com> wrote:
> > Every template instantiation is a new set of code with _zero_ assocation
> > with
> > any other template instantation.
>
> Yeah, I know. But I don't think this has to be set in stone. Having some specific compile-time type information about a template instance would be cool.
True. I think that it could be made possible to get traits information on whether something is an instantation of a particular template without changing how templates work in general, but it just doesn't work that way right now.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation