Get template parameter value

Andrea Fontana via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 29 01:58:49 PDT 2015


On Tuesday, 29 September 2015 at 08:44:03 UTC, Andrea Fontana 
wrote:
> 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);
> }

You can also write void check(alias T) as void check(int T), of 
course.


More information about the Digitalmars-d-learn mailing list