The C ++ 0x standard says:
ยง6.7 Declaration of declaration [stmt.dcl]
4 / Zero initialization (8.5) of all block volume variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization place. Constant initialization (3.6.2) of an object with a block scope with a static storage duration, if applicable, is performed before its block is entered first. Implementations are allowed to perform early initialization of other variables of the block area with static or storage streams of threads under the same conditions that implementations are allowed to statically initialize a variable with statics or storage time of streams in the namespace (3.6.2). Otherwise, such a variable is initialized when the first control passes through its declaration; such a variable is considered initialized after completion of its initialization.
If initialization is completed by throwing an exception, initialization is not completed, so it will be checked again when the next control enters the declaration.
If the control enters the declaration at the same time when the variable is initialized, parallel execution should wait for the initialization to complete. 88
If the control re-enters the declaration recursively during variable initialization, the behavior is undefined.
[Example:
int foo(int i) { static int s = foo(2*i);
-end example]
88) The implementation should not introduce a deadlock around the execution of the initializer.
As expected, it is quite complete.
However, the fact is that even older versions of gcc have already accomplished this and actually even better: an exception is thrown in the case of recursive initialization.
Finally, as for the programmer adding it later: you can usually do this if you have something like Compare And Swap, and use a small enough variable, relying on the variable's zero initialization to mark its uncomputed state. However, I agree to it a lot easier if it is baked.
I am afraid that I have stopped following VC ++, but I do not know where it is located right now. My only advice would be ... see it at build level.
Matthieu M.
source share