Multiple Template Specialization Types (in D1)?

Steven Schveighoffer schveiguy at yahoo.com
Wed Jul 2 07:14:08 PDT 2008


"Jarrett Billingsley" wrote
> "Nick Sabalausky" wrote
>>
>> I'm still somewhat new to D's templates (been spending a lot of time in 
>> non-D development...not by choice), so I'm trying to understand exactly 
>> what's going on here as best as I can. Looked up "IFTI" (Implicit 
>> Function Template Instantiation), so I see now this method (test2.d) does 
>> work, but you have to explicitly instantiate the Foo template when 
>> calling the function:
>>
>> In main:
>> "Foo('a');" -> "Foo!(char)('a');"
>>
>> That works. The first compiler error being from line 1 threw me off, 
>> didn't realize that was just a result of the line 9 error below.
>>
>> But could you explain how it is that Foo stops being a function template? 
>> Or rather, what is the compiler doing when attempting to process line 9 
>> in both the broken "Foo('a');" and fixed "Foo!(char)('a');" scenarios?
>
> From what I understand, the compiler considers a template to be a template 
> function if an only if it has exactly one member named the same thing as 
> itself, and that member is a function declaration.  Aliases are not 
> function declarations so it fails, though one could argue that an alias is 
> simply a reference _to_ another symbol, so this should work.  Of course, 
> none of this is specified.
>
> It can't deduce the template function on line 9 simply because it doesn't 
> think that Foo is a template function.

In that case, this should work, but generates extra code (which should be 
inlined):

public void Foo(T: char)(T arg)  {Foo_dwc!(T)(arg); }
public void Foo(T: wchar)(T arg) {Foo_dwc!(T)(arg); }
public void Foo(T: dchar)(T arg) {Foo_dwc!(T)(arg); }

private void Foo_dwc(T)(T arg) {  }

void main(char[][] args)
{
Foo('a');
}

(not tested)

-Steve




More information about the Digitalmars-d-learn mailing list