metaprograming: staticMap and such?

Dmitry Olshansky dmitry.olsh at gmail.com
Tue Jul 6 14:23:27 PDT 2010


On 07.07.2010 0:35, bearophile wrote:
> Dmitry Olshansky:
>> template staticMap(alias F,T...){
>>       static if(T.length>  1){
>>           alias TypeTuple!(F!(T[0]),staticMap!(F,T[1..$])) staticMap;
>>       }else{
>>           alias TypeTuple!(F!(T[0])) staticMap;
>>       }
>> }
>
> It's better if you add some spaces to improve readability, and generally a map has to work on an empty collection too. "The Little Schemer" book shows that it's often better to base recursivity on zero or empty or the thing that represents nothing (untested):
>
Sure thing, my codestyle is lacking spaces - all that fancy template 
constructs are rather cryptic in C+, looks like I'm grown used to poorly 
readable constructs.

> template staticMap(alias F, T...) {
>      static if (T.length)
>          alias TypeTuple!(F!(T[0]), staticMap!(F, T[1 .. $])) staticMap;
>      else
>          alias TypeTuple!() staticMap;
> }
>
> Bye,
> bearophile

Thanks, that stuff works. An idea of working with empty typetuples never 
crossed my mind, but that, of course, should be covered.

---
Dmitry Olshansky


More information about the Digitalmars-d mailing list