www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Default class template parameter

reply Igor Shirkalin <mathsoft inbox.ru> writes:
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'.
May 27
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
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);
May 27
parent Igor Shirkalin <mathsoft inbox.ru> writes:
On Saturday, 27 May 2017 at 19:30:40 UTC, Stanislav Blinov wrote:
 On Saturday, 27 May 2017 at 19:23:59 UTC, Igor Shirkalin wrote:
 [...]
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);
Thank you!
May 27