www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is expression for template structs/classes instances?

reply d coder <dlang.coder gmail.com> writes:
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
Dec 20 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Dec 21 2010
parent bearophile <bearophileHUGS lycos.com> writes:
 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
Dec 21 2010
prev sibling next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Dec 21 2010
next sibling parent d coder <dlang.coder gmail.com> writes:
 S!int foo;
 static if ( is( typeof(foo) f =3D=3D S!T, T ) ) {
 =A0 =A0// Here, T =3D=3D int, f =3D=3D 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
Dec 21 2010
prev sibling parent reply d coder <dlang.coder gmail.com> writes:
 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
Dec 21 2010
parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
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
Dec 21 2010
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Dec 21 2010