www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Check whether a type is a instantiated by a template struct

reply "Henning Pohl" <henning still-hidden.de> writes:
So the struct is defined as:

struct S(T) {
}

template isS(T) {
     // ...
}

static assert(isS(S!float));
static assert(!isS(float));

There may be some nasty ways using fullQualifiedName!T and so on 
but I guess there is a better way, isn't it?
Aug 11 2012
next sibling parent "Jakob Ovrum" <jakobovrum gmail.com> writes:
On Saturday, 11 August 2012 at 18:51:36 UTC, Henning Pohl wrote:
 So the struct is defined as:

 struct S(T) {
 }

 template isS(T) {
     // ...
 }

 static assert(isS(S!float));
 static assert(!isS(float));

 There may be some nasty ways using fullQualifiedName!T and so 
 on but I guess there is a better way, isn't it?

struct S(T) {} template isS(T : S!U, U) { enum isS = true; } template isS(T) { enum isS = false; } static assert(isS!(S!float)); static assert(!isS!float);
Aug 11 2012
prev sibling next sibling parent "Chris Cain" <clcain uncg.edu> writes:
On Saturday, 11 August 2012 at 18:56:30 UTC, Jakob Ovrum wrote:
 struct S(T) {}

 template isS(T : S!U, U) {
     enum isS = true;
 }

 template isS(T) {
     enum isS = false;
 }

 static assert(isS!(S!float));
 static assert(!isS!float);

Same idea, but doing it with just one template and using static ifs... struct S(T) {} template isS(T) { static if(is(T _ : S!U, U)) enum isS = true; else enum isS = false; } static assert(isS!(S!float)); static assert(!isS!float);
Aug 11 2012
prev sibling next sibling parent "Henning Pohl" <henning still-hidden.de> writes:
On Saturday, 11 August 2012 at 19:06:22 UTC, Chris Cain wrote:
 Same idea, but doing it with just one template and using static 
 ifs...

 struct S(T) {}

 template isS(T) {
     static if(is(T _ : S!U, U))
         enum isS = true;
     else
         enum isS = false;
 }

 static assert(isS!(S!float));
 static assert(!isS!float);

Thank you two. Did not know about the great abilities of is.
Aug 11 2012
prev sibling parent "Jakob Ovrum" <jakobovrum gmail.com> writes:
On Saturday, 11 August 2012 at 19:06:22 UTC, Chris Cain wrote:
 Same idea, but doing it with just one template and using static 
 ifs...

 struct S(T) {}

 template isS(T) {
     static if(is(T _ : S!U, U))
         enum isS = true;
     else
         enum isS = false;
 }

 static assert(isS!(S!float));
 static assert(!isS!float);

I usually prefer overloading for this particular kind of template because of the restriction that is() expressions may only introduce symbols when used in static-if statements. I think the if(blah) a = true; else a = false; pattern is worth avoiding as it leaves a bad taste in the mouth of many programmers.
Aug 11 2012