digitalmars.D.learn - Default class template parameter
- Igor Shirkalin (18/18) May 27 2017 Hi,
 - Stanislav Blinov (8/26) May 27 2017 No, you'd have to at least write
 - Igor Shirkalin (2/11) May 27 2017 Thank you!
 
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 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);
 May 27 2017
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:Thank you![...]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 2017








 
 
 
 Igor Shirkalin <mathsoft inbox.ru>