is expression for template structs/classes instances?
Simen kjaeraas
simen.kjaras at gmail.com
Tue Dec 21 01:35:04 PST 2010
d coder <dlang.coder at gmail.com> 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.
If you know the template you want to check for, isExpression works:
S!int foo;
static if ( is( typeof(foo) f == S!T, T ) ) {
// Here, T == int, f == typeof(foo)
}
Note that the syntax "is ( Type Identifier : TypeSpecialization ,
TemplateParameterList )" is only usable inside static if.
--
Simen
More information about the Digitalmars-d-learn
mailing list