Default class template parameter

Stanislav Blinov via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 27 12:30:40 PDT 2017


On Saturday, 27 May 2017 at 19:23:59 UTC, Igor Shirkalin wrote:
> Hi,
>
> I try to make a class template with single template argument 
> defaulted to some type.
> Is it possible to use the name of class without specification 
> of template argumet (no '!' operator)?
>
> Example:
>
> class ClassName(T=double) {
>     this(T value) { /// do some stuff here
>     }
>     /// some other stuff..
> }
>
>
> void main() {
>     a = ClassName(1.2); /// error:  cannot deduce function from 
> argument types !()(int)
>     a = ClassName!double(1.2); /// OK
> }
>
> It seems the compiler treats 'ClassName' as function, but it is 
> obvious that it should treat it as 'ClassName!double'.

No, you'd have to at least write

auto a = new ClassName!()(1.2);

Or you could define a make function:

auto makeClassName(T = double)(T value) {
     return new ClassName!T(value);
}

auto a = makeClassName(1.2);


More information about the Digitalmars-d-learn mailing list