Thread overview
is expression for template structs/classes instances?
Dec 21, 2010
d coder
Dec 21, 2010
bearophile
Dec 21, 2010
bearophile
Dec 21, 2010
Simen kjaeraas
Dec 21, 2010
d coder
Dec 21, 2010
d coder
Dec 21, 2010
Simen kjaeraas
Dec 21, 2010
Jonathan M Davis
December 21, 2010
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
December 21, 2010
d coder:

> 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 remember someone has shown here the URL of a module that given an instantiated template, it returns the typetuple of its instantiation arguments. This code maybe returns the alias of the template before instantiation too. But I don't remember the name of the module.
The need expressed in this post is very common, so some standard way to do it is necessary in Phobos or in __traits/meta.

Bye,
bearophile
December 21, 2010
> The need expressed in this post is very common, so some standard way to do it is necessary in Phobos or in __traits/meta.

I have added this: http://d.puremagic.com/issues/show_bug.cgi?id=5361

Bye,
bearophile
December 21, 2010
d coder <dlang.coder@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
December 21, 2010
> 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.
>

Thanks Simen

I do know the template. I will try out your solution. Will let you know if I face issues.

Regards
- Cherry
December 21, 2010
> I do know the template. I will try out your solution. Will let you know if I face issues.
>

Simen

It works perfect, And this is exactly what I was looking for. If you see my original post, I also thought this form of "is" expression should work. Just could not get around to the right syntax.

With your help it is working now. I am using a slightly more elaborate check which is obvious but I am writing it here to just let the list know.

static if ( is( typeof(foo) f == S!T, T : int) ) {
  // foo is an object of type S!T
  // where T is convertible to int
}

Thanks once more
Warm Regards
- Cherry
December 21, 2010
d coder <dlang.coder@gmail.com> wrote:

>> I do know the template. I will try out your solution. Will let you
>> know if I face issues.
>>
>
> Simen
>
> It works perfect, And this is exactly what I was looking for. If you
> see my original post, I also thought this form of "is" expression
> should work. Just could not get around to the right syntax.
>
> With your help it is working now. I am using a slightly more elaborate
> check which is obvious but I am writing it here to just let the list
> know.
>
> static if ( is( typeof(foo) f == S!T, T : int) ) {
>   // foo is an object of type S!T
>   // where T is convertible to int
> }
>
> Thanks once more
> Warm Regards
> - Cherry

Glad to be of service.

-- 
Simen
December 21, 2010
On Tuesday, December 21, 2010 02:57:45 d coder wrote:
> > I do know the template. I will try out your solution. Will let you know if I face issues.
> 
> Simen
> 
> It works perfect, And this is exactly what I was looking for. If you see my original post, I also thought this form of "is" expression should work. Just could not get around to the right syntax.
> 
> With your help it is working now. I am using a slightly more elaborate check which is obvious but I am writing it here to just let the list know.
> 
> static if ( is( typeof(foo) f == S!T, T : int) ) {
>   // foo is an object of type S!T
>   // where T is convertible to int
> }

Yes, that sort of thing works. The problem is when you want to know whether an arbitrary type is an instantiation of a particular template. For instance, if you have

struct S(T)
{
}

struct Q(T)
{
}

and you pass S!int to Q - Q(S!int) - Q sees S!int is a specific type, not an instantiation of S, so it's difficult to have a template constraint checking that the type passed to Q is an instantiation of S. However, if you know exactly which instantiation that you're checking for, then it's relatively easy.

- Jonathan M Davis