Extracting template parameters

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Nov 6 10:33:32 PST 2012


On 11/6/12, Simen Kjaeraas <simen.kjaras at gmail.com> wrote:
> In addition to Dan's answer, let me present a general solution:
>
> template InstantiationInfo( T ) {
>      static if ( is( T t == U!V, alias U, V... ) ) {
>          alias U Template;
>          alias V Parameters;
>      } else {
>          static assert(false, T.stringof ~ " is not a template type
> instantiation.");
>      }
> }

I think Kenji fixed this recently because it didn't work several
releases ago. You can now do cool things with template constraints.
For example if you want to limit a template to only allow types that
are instances of a specific template you can do:

struct Foo(T...) { }

struct Bar(T)
    if (is(T x == Foo!X, X...))  // T must be a Foo instance
{
}

struct Other(T...) { }

void main()
{
    alias Bar!(Foo!int) A;  // ok
    alias Bar!(Other!int) B;  // fail
}


More information about the Digitalmars-d-learn mailing list