Are C++ static simple types initialized in order?
My experience tells me that given an object:
class Object
{
private:
static int array[];
public:
Object(int id);
};
int Object::a开发者_如何学编程rray[] = { 2937, 892 };
Object::Object(int id)
{
// do something
}
The initialization of array
will happen before the invocation of any method on Object
or the invocation of any method on any other object in the program whether the object is declared static
or not.
Basically, what I'm asking is does anyone disagree that static simple C types (non-objects) like char
, short
, int
, and long
(and structs without constructors composed of those types) are initialized when an executable is loaded into memory, before main() or any other constructor is invoked?
Yes, all static initialization happens before main() is called. But you cannot be sure in what order the initialization happens. This can cause havoc when one static variable depends on the existence of another. It's called the static initialization fiasco: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.14
I often use the solution mentioned in the faq. Use functions with static variables inside to control the order better.
The specific type of initialization that I think you're talking about:
Basically, what I'm asking is does anyone disagree that static simple C types (non-objects) like char, short, int, and long (and structs without constructors composed of those types) are initialized when an executable is loaded into memory, before main() or any other constructor is invoked?
I assume you mean that the initialization with constants - not by calling a function. If that assumption is correct, then yes you can be sure that initialization will happen before any constructors are called:
- 3.6.2 initialization of non-local objects
Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (3.9) with static storage duration initialized with constant expressions (5.19) shall be initialized before any dynamic initialization takes place. Objects with static storage duration defined in namespace scope in the same translation unit and dynamically initialized shall be initialized in the order in which their definition appears in the translation unit.
However an initalization such as
static int x = getvalue();
falls into the dynamic initialization category, so it gets ordered by appearance (and indeterminately between different translation units).
Another caveat is that this doesn't apply to local statics (which might not be initialized until their block is entered), but that really doesn't matter since they aren't accessible to anything else until the enclosing block has been entered anyway.
This is indeed the case. What is not guaranteed is the order that static members in different translation units are initialised with respect to one another.
I guess it's compiler and compiler-option dependant, but your compiler might very well just put the values in the program at compile time if those values are litteral (written as is in the code, as in your example).
I think any decent compiler will do that. Then when your exe is loaded in memory, it already have the values hard-coded.
The Standard only says that statics are initialized before control enters a scope in which they could be accessed. It doesn't define anything else. I think that file-scope variables are a different thing, but in general, using statics and depending on static behaviour is a bad idea.
精彩评论