Recursive template

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Nov 15 10:49:31 PST 2014


On Saturday, 15 November 2014 at 18:30:00 UTC, Eric wrote:
> Hi -
>
> I've never designed a recursive template before, but I think
> that would solve my problem.  What I would like is
> someting like this:
>
> class X(V, K...)
> {
>     // I want to declare a type based on K and V such
>     // that for X!(V, int, string, double) the resulting
>     // declaration would be:
>     SomeType!(int, SomeType!(string, SomeType!(double, V))) var;
>
>     // or put another way:
>     SomeType!(K[0], SomeType!(K[1], SomeType(K[2], V))) var;
> }
>
> Can anyone give me some ideas on how to set up the declaration?
>
> Thanks,
>
> Eric

It's pretty straight forward:

struct SomeType(K, V) {}

template X(V, K...)
{
      static if(K.length == 1)
          alias X = SomeType!(K[0], V);
      else static if(K.length > 1)
          alias X = SomeType!(K[0], X!(V, K[1 .. $]));
      else static assert(false);
}


More information about the Digitalmars-d-learn mailing list