Behavior of "auto"

NullTerminator NullTerminator at Hotmail.com
Wed Dec 5 22:20:53 PST 2007


== Repost the article of NullTerminator (NullTerminator at Hotmail.com)
== Posted at 2007/12/06 01:07 to D

A strange effect of using auto:

The following code:
import std.stdio;

class Test {
   this() {
      printf("Created\n");
   }

   ~this() {
      printf("Destroyed\n");
   }
}

int main(char[][] args){
   for (int n = 0; n < 10; n++)
      Test t = new Test();
   return 0;
}

produces the following output:
Created
Created
Created
Created
Created
Created
Created
Created
Created
Created
Destroyed
Destroyed
Destroyed
Destroyed
Destroyed
Destroyed
Destroyed
Destroyed
Destroyed
Destroyed

where as changing the line:
      Test t = new Test();
to:
      auto Test t = new Test();
produces:
Created
Destroyed
Created
Destroyed
Created
Destroyed
Created
Destroyed
Created
Destroyed
Created
Destroyed
Created
Destroyed
Created
Destroyed
Created
Destroyed
Created
Destroyed

As I understand it, one would expect this to be the effect of "scope," not
"auto."  Can someone explain why this happens?

Thanks in advance.




More information about the Digitalmars-d mailing list