Passing several tuples (T...) to templates without expanding them

simendsjo simendsjo at gmail.com
Wed Mar 13 10:14:30 PDT 2013


On Wednesday, 13 March 2013 at 15:49:06 UTC, jerro wrote:
>>>>> and a template
>>>>> template t(alias A, alias B) {
>>>>>     // something something
>>>>> }
>>>>>
>>>>> Given
>>>>> alias Tuple!(int, 1) A;
>>>>> alias Tuple!(int, 1) B;
>>>>>
>>>>> Is it possible to send this to template t as follows
>>>>> t!(A, B)
>>>>> without it expanding to
>>>>> t!(int, 1, int, 1)
>>>>> ?
>
> Not as far as I know. You can work that around it by wrapping 
> it in a non eponymous template. I use a template like this for 
> this purpose:
>
> template group(a...){ alias a members; }
>
> then you do
>
> alias A = group!(int, 1);
> alias B = group!(int, 1);
>
> t!(A, B)
>
> And you use A.members and B.members inside t. You could also 
> declare A and B as you did above, and use t like this:
>
> t!(group!A, group!B)

Thanks, that's a good idea, and I'll probably rewrite my code to 
use this.

For my needs I just hardcoded it, stuffing everything in the same 
template.

     template hasEqualAttributes(Recursive recursive, alias A, 
alias B)
     {
         alias AttributeTuple!(recursive, A) AAttr;
         alias AttributeTuple!(recursive, B) BAttr;

         static if(AAttr.length != BAttr.length)
         {
             enum hasEqualAttributes = false;
         }
         else
         {
             template _hasEqualAttributes(int i)
             {
                 static if(i >= AAttr.length)
                     enum _hasEqualAttributes = true;
                 else
                     enum _hasEqualAttributes = isEqual!(AAttr[i], 
BAttr[i]) && _hasEqualAttributes!(i+1);
             }
             enum hasEqualAttributes = _hasEqualAttributes!0;
         }
     }


More information about the Digitalmars-d-learn mailing list