Get class type parameters at compile time
Philippe Sigaud
philippe.sigaud at gmail.com
Thu Dec 13 14:10:30 PST 2012
On Thu, Dec 13, 2012 at 11:56 AM, js.mdnq <js_adddot+mdng at gmail.com> 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
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20121213/bea13408/attachment.html>
More information about the Digitalmars-d-learn
mailing list