Question on Template Specializing.

evilrat evilrat666 at gmail.com
Tue Aug 6 22:30:59 PDT 2013


On Wednesday, 7 August 2013 at 05:10:25 UTC, SteveGuo wrote:
> The following example can't be compiled.
>
> // A.d
> -------------------------------------
>
> class A(T)
> {
>     T a;
> }
>
>
> // main.d
> -------------------------------------
> import A;
>
> int main(string param[])
> {
>     A!(int) a;
>
>     return 0;
> }
>
> Compiler complains with this message "Error: template instance 
> A!(int) A is not a template declaration, it is a import"
>
> So I read online documents, It says "TemplateInstantances are 
> always performed in the scope of where the TemplateDeclaration 
> is declared, with the addition of the template parameters being 
> declared as aliases for their deduced types."
>
> So, I modified the code as the following
>
> // A.d
> -------------------------------------
>
> class A(T)
> {
>     T a;
> }
>
> alias A!(int) AInt;
>
> // main.d
> -------------------------------------
> import A;
>
> int main(string param[])
> {
>     AInt a;
>
>     return 0;
> }
>
> This time it compiled successful.
>
> But my question is:
> If I am a library author, I write templates for my users, so 
> users can specialize the template with any types they defined. 
> I don't know what types they may define when I design the 
> template, so how can I specialize the template for them in my 
> template module?

doh... most of such weird errors are due to module naming. module 
names should be always in lowercase(not neccessary but 
recommended), and the second, your problem is due to name 
conflict of module name and class name, just give your module a 
meaningful name and problem is gone.

why is that? i think because alias has tighter lookup rules than 
"normal" code.


More information about the Digitalmars-d mailing list