How to add n items to TypeTuple?

Justin Whear justin at economicmodeling.com
Thu Nov 1 11:52:34 PDT 2012


On Thu, 01 Nov 2012 19:42:07 +0100, denizzzka wrote:

> For example, adding 3 strings to type tuple t:
> 
> foreach( i; 0..2 )
>   alias TypeTuple!( t, string ) t; // this is wrong code
> 
> and result should be:
> 
> TypeTuple!( string, string, string );

Use a recursive template.  Here's one that repeats a given type N times:

template Repeat(Type, size_t Times)
{
    static if (Times == 0)
        alias TypeTuple!() Repeat;
    else
        alias TypeTuple!(Type, Repeat!(Type, Times - 1)) Repeat;
}

Invoke like so:
Repeat!(string, 3) threeStrings;


More information about the Digitalmars-d-learn mailing list