Check Instance of Template for Parameter Type/Value

Justin Whear via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Oct 19 08:26:04 PDT 2015


On Mon, 19 Oct 2015 14:51:28 +0000, Stewart Moth wrote:

> I'm working with a library that has template structs of mathematical
> vectors that can sometimes be the type of an array I'm passing to a
> function.
> 
> The definition of the struct is like this:
> 
> struct Vector(type, int dimension_){ ... }
> 
> Where type is going to be an int/float/etc and dimension_ is 2/3/4.
> 
> I could write a bunch of functions for each case, but I'd rather not...
> I'd like to use something like the following:
> 
> void foo(T)(Array!T array){
>      if(isInstanceOf!(Vector, T)){
>          //get type of T or check it //test if dimension_ is 2 or 3 or 4
>          ...
>      } else {
>          //Non-vector stuff ...
>      }
> }
> 
> But to do that I need to check that parameters of T as if it were an
> instantiated instance of Vector and I'm not sure how to accomplish
> that... Can anyone help me out with what I need to do?

Pattern matching!

void foo(ArrayT : Array!VectorT, VectorT : Vector!(T, dimension), T, int 
dimension)(ArrayT arr)
{
	static if (dimension >= 2 && dimension <= 4)
	{

	} else {

	}
}


More information about the Digitalmars-d-learn mailing list