www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Static in templates, member scope

// Static variable in template member changes only one time.

class A {
   void foo() {
     static int x = 0;
     x++;
     printf("%d", x);
   }
}
class B(T) {
   void foo() {
     static int x = 0;
     x++;
     printf("%d", x);
   }
}
void main() {
   A a = new A(); B!(int) b = new B!(int)();
   a.foo(); a.foo(); a.foo(); printf("\n"); // outputs 123
   b.foo(); b.foo(); b.foo(); printf("\n"); // outputs 111
}

// If declare 'static int x = 0' in class (or template) scope, 'B' output 
is "123".
May 12 2004