template struct question

WhatMeWorry kheaser at gmail.com
Fri Feb 14 04:21:59 UTC 2025


Just wanted to thank everybody for their suggestions. Ali's code 
provided the breakthrough:

```d
import std.stdio;
void main()
{

struct HexBoard(F,I)
{
     this(F d, I r, I c) { diameter = d; rows = r; cols = c; }
     F diameter;
     I rows;
     I cols;
}

void displayHexBoard(HB)(HB h) {
     writeln(typeof(h.diameter).stringof);
     writeln(typeof(h.rows).stringof);
}

auto h1 = HexBoard!(float,uint)(.25, 3, 7);
auto h2 = HexBoard!(double,int)(.3, 5, 5);

displayHexBoard(h1);
displayHexBoard(h2);
}
```

which returned

float
uint
double
int

My problem was with the definition of void displayHexBoard(HB)(HB 
h) {} definition.

I kept futzing around with trying to insert HexBoard(F,I) into 
where HB is.  It is incredibly elegant how D can take h1 or h2 
and instantiate the template HB.


More information about the Digitalmars-d-learn mailing list