Get struct template types

ed growlercab at gmail.com
Tue Feb 4 03:47:27 PST 2014


On Tuesday, 4 February 2014 at 11:01:01 UTC, Jakob Ovrum wrote:
> On Tuesday, 4 February 2014 at 09:30:22 UTC, ed wrote:
>> Hi,
>>
>> given a struct like so:
>>
>> struct S(alias N, T) {...}
>>
>> is there a way to get the template parameters of S? Something 
>> like:
>>
>> S.typetuple[0] == N,
>> S.typetuple[1] == T
>>
>> I've had a look at std.typecons and std.typetuple but I don't 
>> see what I'm missing something and cannot see a way to do the 
>> above.
>>
>> Thanks,
>> ed
>
> Use type deduction:
>
> ---
> struct S(size_t n, T) {}
>
> enum isS(T) = is(T == S!(n, U), size_t n, U);
>
> template isS(T, size_t n)
> {
>     static if(is(T == S!(n2, U), size_t n2, U))
>         enum isS = n == n2;
>     else
>         enum isS = false;
> }
>
> alias MyS = S!(3, float);
>
> static assert(isS!MyS);
> static assert(isS!(MyS, 3));
> static assert(is(MyS == S!(3, float)));
> ---

This is it!

Thanks,
ed


More information about the Digitalmars-d-learn mailing list