Why defining alias and not auto when using a template?
monarch_dodra
monarchdodra at gmail.com
Fri Apr 4 06:28:40 PDT 2014
On Friday, 4 April 2014 at 13:23:48 UTC, Bienlein wrote:
> Hello,
>
> I took some code snippet from some sample D code and modified
> it a bit:
>
> template TCopy(T, V) {
> private int i = 2;
>
> void copy(out T to, out V to2, T from) {
> to = from;
> to2 = from;
> writeln("i: ", i);
> }
> }
>
> void main(string[] args)
> {
> int x = 2;
> int y = 2;
> alias myCopy = TCopy!(int, int);
> myCopy.copy(x, y, 37);
> writeln("x: ", x, " y: ", y);
> }
>
> My question is now why I have to declare and alias as in
>
> alias myCopy = TCopy!(int, int);
>
> If I define auto instead of alias, it does not compile. My
> question is why defining auto does not work. I would consider
> this more intuitive.
>
> Thanks, Bienlein
"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.
More information about the Digitalmars-d-learn
mailing list