www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Unused template arguments; what type to use?

reply drathier <forum.dlang.org fi.fo> writes:
How can I tell the compiler that I will never create a value of 
type X, while still being able to write code that uses it? Using 
void as a template parameter is where I started, but I still need 
to be able to declare variables inside this unreachable function, 
like `T foo;` even when `T == void`. Can I get any closer to what 
I want than an empty struct?
Jun 26 2020
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
On Friday, 26 June 2020 at 13:21:25 UTC, drathier wrote:
 How can I tell the compiler that I will never create a value of 
 type X, while still being able to write code that uses it? 
 Using void as a template parameter is where I started, but I 
 still need to be able to declare variables inside this 
 unreachable function, like `T foo;` even when `T == void`. Can 
 I get any closer to what I want than an empty struct?
Depends on what you care about, I guess. A final abstract class has been my go-to on a few occasions. I'd argue the empty struct is better in most cases, and a forward-declared struct with no implementation might work in some cases. -- Simen
Jun 26 2020
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Jun 26, 2020 at 01:40:57PM +0000, Simen Kjærås via Digitalmars-d-learn
wrote:
 On Friday, 26 June 2020 at 13:21:25 UTC, drathier wrote:
 How can I tell the compiler that I will never create a value of type
 X, while still being able to write code that uses it? Using void as
 a template parameter is where I started, but I still need to be able
 to declare variables inside this unreachable function, like `T foo;`
 even when `T == void`. Can I get any closer to what I want than an
 empty struct?
Depends on what you care about, I guess. A final abstract class has been my go-to on a few occasions. I'd argue the empty struct is better in most cases, and a forward-declared struct with no implementation might work in some cases.
[...] You can also use void[0], which has size 0, unlike an empty struct which has size 1. T -- The best compiler is between your ears. -- Michael Abrash
Jun 26 2020