Classes and templates

Meta via Digitalmars-d digitalmars-d at puremagic.com
Mon Oct 31 13:20:09 PDT 2016


On Monday, 31 October 2016 at 20:06:22 UTC, Hefferman wrote:
> On Monday, 31 October 2016 at 19:43:57 UTC, Adam D. Ruppe wrote:
>> On Monday, 31 October 2016 at 19:24:46 UTC, Hefferman wrote:
>>
>> Give a template type when crating it
>>
>> new BubbleSort!(string)
>>
>> for example
>
> Is it possible to create this using "typeof"?
> E. g. "BubbleSort b = new BubbleSort!(typeof(arr));"
> Compilation fails with that line....

You can't use an un-instantiated template as a type anyway, 
unlike Java. There the above is illegal because of `BubbleSort b 
= ...`. The symbol BubbleSort by itself is not a type; you have 
to instantiate it with template arguments to create a type. So 
you *could* do it like this:

BubbleSort!(typeof(arr)) b = new BubbleSort!(typeof(arr));

But that's too verbose. There's need need to repeat ourselves. 
You can instead use `auto` and omit the type of `b`, which will 
be inferred from the right hand side:

auto b = new BubbleSort!(typeof(arr));


More information about the Digitalmars-d mailing list