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

monarch_dodra monarchdodra at gmail.com
Wed Mar 13 04:13:54 PDT 2013


On Wednesday, 13 March 2013 at 10:34:14 UTC, simendsjo wrote:
> Say you have a tuple type:
>     struct Tuple(T...) {
>         alias T Tuple;
>     }
>
> 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)
> ?
>
> This is what I'm trying to achieve, but encapsulated in a 
> template:
>     alias Tuple!(int, "aoeu", short) A;
>     alias Tuple!(int, "aoeu", short) B;
>     foreach(i, T; A) {
>         pragma(msg, i, " ", T);
>         pragma(msg, isEqual!(T, B[i]));
>     }

I have trouble understanding the question. Perhaps you are 
confusing Tuple and TypeTuple?

TypeTuples are not actual types, and always expanded.
Tuple is an actual type, and doent expand unless requested via 
expand.

As for your code, it is a bit confusing, because most of it 
doesn't compile...

In any case, this code:
//----
     alias Tuple!(int,  int)  A;
     alias Tuple!(uint, uint) B;

     void foo(Args...)()
     {
         foreach(i, Type; Args)
             writefln("Type %d: %s.", i, Type.stringof);
     }
     foo!(A, B)();
//----
produces
//----
Type 0: Tuple!(int, int).
Type 1: Tuple!(uint, uint).
//----
Is this what you want?

Also, if you want to extract a type out of a tuple, you need to 
use "Type":

//----
      alias Tuple!(int, "aoeu", short) A;
      alias Tuple!(int, "aoeu", short) B;

      foreach(i, T; A.Types)
      {
          pragma(msg, "A ", i, " ", T.stringof);
          pragma(msg, "B ", i, " ", B.Types[i].stringof);
          pragma(msg, is(T == B.Types[i]));
      }
//----
This works
//----
A 0u int
B 0u int
true
A 1u short
B 1u short
true
//----

Writing A[0] will just tell you that "need 'this' to access 
member". Because pragma errors aren't blocking errors, things get 
confusing.


More information about the Digitalmars-d-learn mailing list