is expression for template structs/classes instances?
Jonathan M Davis
jmdavisProg at gmx.com
Mon Dec 20 21:22:03 PST 2010
On Monday 20 December 2010 20:23:49 d coder wrote:
> Greetings
>
> I want to find if a given struct type is instantiated from a
> particular template struct type. For example:
>
> struct S (T) {
> alias T Type;
> T t;
> }
>
> And later I want to find out if a given type is of type S(*)
> (basically any type instantiated from template struct S). In fact I do
> not know the type value T used at the time of instantiating S!(T).
>
> I was looking at "is ( Type Identifier : TypeSpecialization ,
> TemplateParameterList )" expression at
> http://www.digitalmars.com/d/2.0/expression.html#IsExpression .
> Thought there would be some way using that, but I could not find any.
>
> Regards
> Cherry
Well, from the compiler's perspective S!int would have no relation to S!float,
S!bool, or any other S!T. The template is instantiated with whatever types and
values that it's given and then it's its own beast. So, really, there is no
relation between the various instantiations of any particular template. I'm not
sure that it would be impossible to have something in __traits or std.traits
which tested whether a particular type was an instantiation of a particular
template, but I'm not at all certain that it _is_ possible. Templates are used
to generate code, but once generated, that code is essentially the same as it
would have been had you typed it all yourself. So, my guess would be that you
can't do what you're trying to do. I agree that it could be useful to be able to
do it, but unfortunately, I don't think that it's possible.
If you knew enough about the type, you might be able to do some template voodoo
to do it in a round-about manner, but it would be specific to the type in
question. For instance, given your definiton of S, you could use
_traits/std.traits to check that the type that you're testing has a member
variable t. You could then check that S!(typeof(t)) was the same as the type
that you were testing. So, if you get particularly cunning about it, I believe
that it can be tested for in specific cases, but I don't believe that it can be
done in any general way.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list