Why defining alias and not auto when using a template?

Steven Schveighoffer schveiguy at yahoo.com
Fri Apr 4 06:49:10 PDT 2014


On Fri, 04 Apr 2014 09:35:57 -0400, Bienlein <jeti789 at web.de> wrote:

>
>> "auto" is used to declare an instance, or an object.
>> "alias" is used to declare a name.
>>
>> What you are currently doing is saying "the function TCopy!(int, int)  
>> can now be refered to as myCopy". You aren't actually creating any data.
>
> All right, thanks. Then I create an instance:
>
> auto myCopy = new TCopy!(int, int);	
> alias myCopy = new TCopy!(int, int);	
>
> Neither nor compiles now. How can? Seems to me a template is not a class  
> in that sense ?!

A template is not a structure or class, it is a template. It's basically  
lexically checked, but not semantically. Once you define it's types and  
parameters, then the compiler basically substitutes those in, and compiles  
the result.

Note that a class template:

class C(T)
{
}

is short for this:

template C(T)
{
    class C
    {
    }
}

-Steve


More information about the Digitalmars-d-learn mailing list