www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why are default template parameters not permitted on class templates?

reply JN <666total wp.pl> writes:
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
parent Adam D. Ruppe <destructionator gmail.com> writes:
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.
Nov 29 2020