digitalmars.D.learn - Why are default template parameters not permitted on class templates?
- JN (12/12) Nov 29 2020 class ValueHolder(T = int)
- Adam D. Ruppe (12/13) Nov 29 2020 Make it `ValueHolder!()` and it works.
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)
Nov 29 2020
On Sunday, 29 November 2020 at 21:52:10 UTC, JN wrote:ValueHolder v2; // errorMake 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.
Nov 29 2020