Help needed to learn templates

Vinod K Chandran kcvinu82 at gmail.com
Sat Mar 19 05:54:26 UTC 2022


Hi all,
I am trying to learn D templates with Philippe Sigaud's "D 
Templates: A Tutorial". So far so good. I have completed first 19 
pages and in the 20th page, I found an obstacle. This is the code.
```d
module rank1;

template rank(T)
{
     static if (is(T t == U[], U)) // is T an array of U, for some 
type U?
         enum size_t rank = 1 + rank!(U); // then let’s recurse 
down.
     else
     enum size_t rank = 0; // Base case, ending the recursion.
}

module using_rank1;
import rank1;
static assert(rank!(int) == 0);
static assert(rank!(int[]) == 1);
static assert(rank!(int[][]) == 2);
static assert(rank!(int[][][]) == 3);
```
Question 1 - `U` is appearing in the first static if statement. 
But we had to write `U` on the template line, right? Like - 
`template rank(T, U)`
Question 2 - The statif if test is - `T t == U[ ]` What does that 
mean ?
Question 3 - if `T t == U[ ]` is the test, then I think when we 
pass
```d
rank!(int[ ][ ][ ])
```
The test will be `int[ ][ ][ ] == U[ ]`, Right ?


More information about the Digitalmars-d-learn mailing list