ALiX wrote:
On Jul 26, 8:10 pm, terminator <farid.mehr...@gmail.com> wrote:
the order of initialization of static variables depends on link
order of modules and definition order of them in a module.
In general this is some nasty behavior that makes things complex.I
am not certain that this was the reason for your problem
I actually think that this specific case is a compiler issue. The
point is that the entire code is in a single translation unit, and so
the order of initialization is the order in which the variables are
defined. Therefore, NONE should be initialized before the global
variable a.
The problem is that 'NONE' in your original code was a template (it
was a member of a template, and therefore a template itself). Those
aren't initialised were _defined_, they are initialised when first
referenced (IOW, when *instantiated*) [temp.inst]/1, /7. Since your
use of 'NONE' required (expected) it to be instantiated, you get into
the chicked-and- egg problem. To solve it, you could create a dummy
object before the declaration of the 'a' and initialise it with the
NONE member:
unsigned const dummy = A<double>::NONE;
then if you happen to instantiate A<double> later, it will have access
to previously instantiated (and properly initialised) NONE member:
A<double> a;
...
Curiously enough, it might just be QoI issue. I built your code with
VC++ 2005 SP1. In Debug mode it gave me your output, and in Release
mode it gave the correct output... Go figure.
as in Release.
I've not tried other compilers.