Get class type parameters at compile time

Philippe Sigaud philippe.sigaud at gmail.com
Fri Dec 14 12:14:19 PST 2012


On Fri, Dec 14, 2012 at 1:18 AM, bearophile <bearophileHUGS at lycos.com>wrote:

>
> Fit to be added to Phobos?
>

Maybe, I don't know. People seem to ask for this quite regularly.

Here is a slightly improved version, what do you think?

template isTemplatedType(Type...) if (Type.length == 1)
{
    mixin("alias " ~ Type[0].stringof ~ " U;");
    static if (is(Type[0] _ == U!Args, Args...))
        enum isTemplatedType = true;
    else
        enum isTemplatedType = false;
}

template TemplateArity(Type...) if (Type.length == 1 &&
isTemplatedType!(Type[0]))
{
    mixin("alias " ~ Type[0].stringof ~ " U;");
    static if (is(Type[0] _ == U!Args, Args...))
        enum TemplateArity = Args.length;
}

template TemplateArgs(Type...) if (Type.length == 1 &&
isTemplatedType!(Type[0]))
{
    mixin("alias " ~ Type[0].stringof ~ " U;");
    static if (is(Type[0] _ == U!Args, Args...))
        alias Args TemplateArgs;
}

void main()
{
    alias Tuple!(int, double) T;
    writeln(TemplateArity!T);
    writeln(TemplateArgs!T.stringof);
    writeln(isTemplatedType!T);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20121214/a1eb6c1a/attachment.html>


More information about the Digitalmars-d-learn mailing list