...
Wiki Markup |
---|
\[[ISO/IEC 14882-2003|AA. C++ References#ISO/IEC 14882-2003]\] Section 6.7, "Declaration Statement" describes the initialization of static and thread storage duration objects. InThe thedirect casequote ofis static objects, recursive reentry into the initialization of a static storage duration causes undefined behavior and various results can be obtained when using different compilersas follows: |
The zero-initialization of all local objects with static storage duration or thread storage duration is performed before any other initialization takes place. Constant initialization of a local entity with static storage duration, if applicable, is performed before its block is ?rst entered. An implementation is permitted to perform early initialization of other local objects with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize an object with static or thread storage duration in namespace scope. Otherwise such an object is initialized the ?rst time control passes through its declaration; such an object is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the object is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the object is being initialized, the behavior is unde?ned.
Noncompliant Code Example
...
Code Block | ||
---|---|---|
| ||
int test(int x){ x--; if(x < 0 || x > 10) { return 0; } else { static int y = test(x); //<--undefined behavior occurs here return y; } } |
...
Implementation-Specific Details
In gcc3, this code will recurse as if y
were a non-static variable.
...