www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to add n items to TypeTuple?

reply "denizzzka" <4denizzz gmail.com> writes:
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 );
Nov 01 2012
next sibling parent Justin Whear <justin economicmodeling.com> writes:
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;
Nov 01 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2012-11-01, 19:52, Justin Whear wrote:

 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;

I've always preferred the opposite order of arguments, as that allows repetition of more complex things: template Repeat(size_t times, T...) { static if ( times == 0 ) { alias TypeTuple!() Repeat; } else { alias TypeTuple!( T, Repeat!( times - 1, T ) ) Repeat; } } Invoke like so: alias Repeat!(4, string, int, "Hello, template world!") YeahThisIsGonnaBeUseful; Or: alias Repeat!(3, string) ThreeStrings; -- Simen
Nov 01 2012
prev sibling parent "denizzzka" <4denizzz gmail.com> writes:
Great! Thanks!
Nov 01 2012