| Thread overview | |||||
|---|---|---|---|---|---|
|
May 27, 2017 Default class template parameter | ||||
|---|---|---|---|---|
| ||||
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 Re: Default class template parameter | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Igor Shirkalin | 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 Re: Default class template parameter | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | 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!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply