C
Christof Warlich
Hi,
the few lines of code below show different results depending on
the compiler version (gcc-2.95 versus gcc-3.3 and later):
gcc-2.95 first initializes d (line 9) and then t (line 8), as
running the executable yields
initialized d
X constructor
whereas gcc-3.3 and later gcc versions do as I'd expect: They
initialize the variables as they appear in the source code:
X constructor
initialized d
Could anyone tell if the initialization sequence is specified
by the standard in such a case or if I cannot rely on the
initialization sequence even when everything is in the same translation
unit?
Thanks,
Christof
1 #include <stdio.h>
2 struct X {
3 X() {printf("X constructor\n");}
4 };
5 template<typename T> struct A {
6 static T t;
7 };
8 template<typename T> T A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initialized d\n");
11 int main() {}
the few lines of code below show different results depending on
the compiler version (gcc-2.95 versus gcc-3.3 and later):
gcc-2.95 first initializes d (line 9) and then t (line 8), as
running the executable yields
initialized d
X constructor
whereas gcc-3.3 and later gcc versions do as I'd expect: They
initialize the variables as they appear in the source code:
X constructor
initialized d
Could anyone tell if the initialization sequence is specified
by the standard in such a case or if I cannot rely on the
initialization sequence even when everything is in the same translation
unit?
Thanks,
Christof
1 #include <stdio.h>
2 struct X {
3 X() {printf("X constructor\n");}
4 };
5 template<typename T> struct A {
6 static T t;
7 };
8 template<typename T> T A<T>::t;
9 X &x = A<X>::t;
10 int d = printf("initialized d\n");
11 int main() {}