void main()
{
struct HexBoard(F,I)
{
this(F d, I r, I c) {}
//void displayHexBoard(HexBoard!(F,I) h) {} // this compiles fine!
}
void displayHexBoard(HexBoard!(F,I) h) {} // error undefined identifier F and I
auto h1 = HexBoard!(float,uint)(.25, 3, 7);
auto h2 = HexBoard!(double,int)(.3, 5, 5);
}
Is there any clever way to move the method outside of a templated block structure? Or is this as good as it gets.
I tried
// Explicit instantiation for 'int' type
//int add!(int, int);
void displayHexBoardData(HexBoard2!(float,uint) h); // declaration
but that just returns
Error: declaration `displayHexBoard` is already defined
onlineapp.d(10): `function` `displayHexBoard` is defined here
Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
February 12 template struct question | ||||
---|---|---|---|---|
| ||||
February 12 Re: template struct question | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry | On 2/12/25 12:55 PM, WhatMeWorry wrote: > struct HexBoard(F,I) > { > this(F d, I r, I c) {} > //void displayHexBoard(HexBoard!(F,I) h) {} // this compiles fine! > } > > void displayHexBoard(HexBoard!(F,I) h) {} // error undefined identifier > F and I Would isInstanceOf be useful? (Which is actually optional.) It necessitated two aliases in the struct: void main() { struct HexBoard(F, I) { alias Float = F; alias Int = I; this(F d, I r, I c) {} } import std.traits : isInstanceOf; void displayHexBoard(HB)(HB h) if (isInstanceOf!(HexBoard, HB)) { pragma(msg, "The types are ", HB.Float, " and ", HB.Int); } auto h1 = HexBoard!(float,uint)(.25, 3, 7); displayHexBoard(h1); } Ali |
February 12 Re: template struct question | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry | On Wednesday, 12 February 2025 at 20:55:14 UTC, WhatMeWorry wrote: >
it can be inferred at the call site, but template arguments must still be declared |
February 13 Re: template struct question | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry | On Wednesday, 12 February 2025 at 20:55:14 UTC, WhatMeWorry wrote: >
Is there any clever way to move the method outside of a templated block structure? Or is this as good as it gets. The problem is that the compiler doesn't know what F and I are. They are placeholders, but you didn't give them a definition.
-Steve |
February 14 Re: template struct question | ||||
---|---|---|---|---|
| ||||
Posted in reply to WhatMeWorry | On Wednesday, 12 February 2025 at 20:55:14 UTC, WhatMeWorry wrote: >
|
February 14 Re: template struct question | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Just wanted to thank everybody for their suggestions. Ali's code provided the breakthrough:
which returned float 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. |