Help needed to learn templates

Salih Dincer salihdb at hotmail.com
Sat Mar 19 08:49:02 UTC 2022


On Saturday, 19 March 2022 at 05:54:26 UTC, Vinod K Chandran 
wrote:
> 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

You don't need anything extra while using it...

I think U is declare a range. Here is a recursive pattern. Just 
like the code I wrote below:

```d
import std.stdio;

alias outer O;

struct outer {
   int i;
   O * o;
}

int rank(T)(T* s) {
   int count = 1;

   if(s.o is null) return count;


   return count + rank(s.o);
}

void main() {
   auto test = O(1, new O(2, new O(3, new O)));

   rank(test.o).writeln;

   test.i.write(", ");
   test.o.i.write(", ");
   test.o.o.i.writeln;
} /* CONSOLEOUT:
3
1, 2, 3
*/
```
SDB at 79


More information about the Digitalmars-d-learn mailing list