Get template parameter value

rumbu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 29 00:50:39 PDT 2015


Having a template:

struct SomeStruct(int size)
{

}

Is there any language trait returning the value of size template 
parameter for the template instantiation SomeStruct!10?

In fact, I'm interested in an eponymous template to test if some 
type is a template inttantation for SomeStruct(int size).

template isSomeStruct(T)
{
   enum isSomeStruct = ?
}

template getStructSize(T) if (isSomeStruct!T)
{
   enum getStructSize = ?
}


I know that I can do something like this:

struct SomeStruct(int size)
{
    enum internalSize = size;
}

template isSomeStruct(T)
{
   enum isSomeStruct = is (typeof(T.internalSize): int);
}

template getStructSize(T) if (isSomeStruct!T)
{
   enum getStructSize = T.internalSize;
}

but I wonder that if it's not another simple and safe way. This 
approach is not safe because there is a risk to have 
SomeOtherStruct(int size) defined with similar semantics.

Thanks.



More information about the Digitalmars-d-learn mailing list