Template parameters that don't affect template type

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 11 11:51:54 PDT 2016


On 8/11/16 2:42 PM, Steven Schveighoffer wrote:
> On 8/11/16 2:11 PM, Engine Machine wrote:
>> I have the need, in some cases, to pass static information to a template
>> class but don't want it to affect its type.
>>
>> import std.algorithm, core.stdc.stdlib;
>> struct X(int defaultSize = 100)
>> {
>>    int Size;
>>    int* p;
>>    void foo(int size)
>>    {
>>     Size = max(size, defaultSize);
>>     p = cast(int*)malloc(Size);
>>    }
>> }
>>
>> If I do
>>
>> X!100 x;
>> X!100 y;
>> X!50 z;
>>
>> then I can do
>>
>> x = y;
>>
>> but not
>>
>> x = z;
>>
>> but of course D things these are completely different types. The type it
>> self does not depend on the default size.
>
> And they should be different types. The code generated for the type is
> different, in this case it's important to declare these are not the same
> type.
>
> For example, if x = y worked, then what should x.foo(5) do?

Of course, I meant x = z :)

-Steve


More information about the Digitalmars-d-learn mailing list