Get template parameter value

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 29 02:11:13 PDT 2015


On Tuesday, 29 September 2015 at 07:50:42 UTC, rumbu wrote:
> 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?

This should do it (untested):

template SomeStructSize(T)
{
     static if(is(T == SomeStruct!n, n))
         enum SomeStructSize = n;
     else static assert(false, T.stringof ~ " is not an instance 
of SomeStruct");
}

Welcome to the weird and wonderful work of  
http://dlang.org/expression.html#IsExpression


More information about the Digitalmars-d-learn mailing list