www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Variadic template problems..?

reply simendsjo <simen.endsjo pandavre.com> writes:
I'm trying to create an array of arrays in a templated struct, but I'm 
having some problems.
Given the struct below, I want to construct an "S.Array[3]" array.
My first template solution, T, also doesn't compile.. Why?

struct S(int C, int R, E) {
     alias E[R][C] Array;
}

/+
// This gives me an syntax error, but I have no idea why
// "no identifier for declarator A[0]"
template T(A...) {
     alias A[0].Array[A.length] T;
}
+/

// This works though..
template U(T, A...) {
     alias T.Array[A.length+1 /+include+/] U;
}

unittest {
     alias S!(1,1,float) S1;
     alias U!(S1, S1) Arr; // I want float[1][1][2]

     static assert(Arr.length == 2); // ok
     writeln(typeid(Arr[0])); // float[1][1][2][0] ... shouldn't this be 
float[1][1][2]?
     writeln(typeid(typeof(Arr[0]))); // float[1][1] ... What happens here?
     writeln(typeid(S1.Array)); // float[1][1]
}
Jun 25 2011
parent simendsjo <simen.endsjo pandavre.com> writes:
On 25.06.2011 18:08, simendsjo wrote:
 I'm trying to create an array of arrays in a templated struct, but I'm
 having some problems.
 Given the struct below, I want to construct an "S.Array[3]" array.
 My first template solution, T, also doesn't compile.. Why?

 struct S(int C, int R, E) {
 alias E[R][C] Array;
 }

 /+
 // This gives me an syntax error, but I have no idea why
 // "no identifier for declarator A[0]"
 template T(A...) {
 alias A[0].Array[A.length] T;
 }
 +/

 // This works though..
 template U(T, A...) {
 alias T.Array[A.length+1 /+include+/] U;
 }

 unittest {
 alias S!(1,1,float) S1;
 alias U!(S1, S1) Arr; // I want float[1][1][2]

 static assert(Arr.length == 2); // ok
 writeln(typeid(Arr[0])); // float[1][1][2][0] ... shouldn't this be
 float[1][1][2]?
 writeln(typeid(typeof(Arr[0]))); // float[1][1] ... What happens here?
 writeln(typeid(S1.Array)); // float[1][1]
 }
I think the error in T might be a bug. The following works: template T2(A...) { alias A[0] First; alias First.Array[A.length] T; } writeln(typeid(Arr2.T)); // Now this actually prints float[1][1][2] as I wanted!
Jun 25 2011