Optional type parameter on a template

Adam D. Ruppe destructionator at gmail.com
Tue May 12 20:40:35 UTC 2020


On Tuesday, 12 May 2020 at 20:36:22 UTC, Luis wrote:
> I'm trying to make a SparseSet that on function of a optional 
> type parameter, could alongside the index set, store other 
> data. So I need a way to declare a optional type template 
> parameter.

A default argument of void is a common way to do it

template foo(T = void) {
    static if(is(T == void)) { not given } else { use T }
}

>     // trying to use ZeroOrMore() gives error : struct 
> onlineapp.ZeroOrMore cannot deduce function from argument types 
> !()(), candidates are: onlineapp.d(12): ZeroOrMore(T = uint, 
> Types...)

Yeah, a template with default parameters must still be 
instantiated, but you do not need to give the arguments. The ! is 
a must though (or you could provide some alias but then it has a 
separate name)


More information about the Digitalmars-d-learn mailing list