November 29, 2020 Why are default template parameters not permitted on class templates? | ||||
---|---|---|---|---|
| ||||
class ValueHolder(T = int) { T t; } void main() { ValueHolder!int v1; ValueHolder v2; // error } onlineapp.d(9): Error: template class onlineapp.ValueHolder(T = int) is used as a type without instantiation; to instantiate it use ValueHolder!(arguments) |
November 29, 2020 Re: Why are default template parameters not permitted on class templates? | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | 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.
|
Copyright © 1999-2021 by the D Language Foundation