Why are default template parameters not permitted on class templates?

Adam D. Ruppe destructionator at gmail.com
Sun Nov 29 22:00:19 UTC 2020


On Sunday, 29 November 2020 at 21:52:10 UTC, JN wrote:
>     ValueHolder v2; // error

Make it `ValueHolder!()` and it works.

Default template params are only considered *after* it is clear a 
template needs to be instantiated. `ValueHolder` by itself is 
just the name of the template which is still distinct from the 
instance. The !() tells it you want an instance, then it will 
fill in default params for the missing items.

You might want to do like

class ValueHolderT!(T = int) {}

alias ValueHolder = ValueHolderT!();

if you want users to use the naked name while keeping the other 
thing available.


More information about the Digitalmars-d-learn mailing list