On Thursday, 13 December 2012 at 22:10:40 UTC, Philippe Sigaud
wrote:
On Thu, Dec 13, 2012 at 11:56 AM, js.mdnq
<[email protected]> wrote:
I need to get the number of type parameters of a class at
compile time:
class a(T1, T2, ...)
{
static if (a.TypeInfo.NumParameters == 1)
....
else
....
}
Is this possible?
Yes, it's possible:
// Inside the class, it's easy:
class Test(T...)
{
enum num = T.length;// T is known here
}
// Outside the class, it's a bit tricky, but quite doable:
template TemplateArity(Type)
{
enum T = Type.stringof;
mixin("alias " ~ T ~ " U;");
static if (is(Type _ == U!Args, Args...))
enum TemplateArity = Args.length;
else
enum TemplateArity = -1;
}
void main()
{
alias Test!(int, double, string) T;
assert(T.num == 3); // Internal knowledge
assert(TemplateArity!T == 3); // External deduction
}
cool, I'll need it outside the class. Thanks.