Get template parameter value

Andrea Fontana via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 29 01:44:02 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?

Something like this is ok?

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

template isSomeStruct(alias S)
{
	void check(alias T)(SomeStruct!T val) { }
	enum isSomeStruct = __traits(compiles, check(S));
}

template getStructSize(alias S) if (isSomeStruct!S)
{
	enum getStructSize = S.structSize;
}

void main()
{
	import std.stdio;
	SomeStruct!10 test;
	writeln(isSomeStruct!test);
	writeln(getStructSize!test);
}




More information about the Digitalmars-d-learn mailing list