the old posts listed the correct authors, but that's what we get for flirting with WordPress.)
A while ago I wrote a lot trying to explain how the hell global static construction works in C++.
The best simple summary I can give you is:
- It doesn't do what you think.
- Your code will explode in ways you didn't expect.
Like peanut and chocolate, these two ideas go well together. That is...static construction problems can be fixed by using intrinsic linked lists. This code is guaranteed to make your life hell:
class foo {
static set
foo() { all_of_me.insert(this); }
~foo() { all_of_me.erase(this); }
// more tuff
};
The idea is that the foo class self-tracks all its members...this is all good until you do this in some other CPP file besides the CPP where all_of_me is defined.
static foo this_static_var_will_kill_me;
First the solution, then why it works. The solution is simply this:
class foo {
static foo * first_of_me;
foo * next;
foo() { this->next = first_of_me; first_of_me = this; }
// more stuff
};
Now foo uses an intrinsic linked list instead of an STL set and we can declare static global foo objects all the time!
Analysis:
- The problem with static construction is that C++ doesn't guarantee that static global objects will be initialized in any particular order.
- When a class has a static member variable that is in turn a complex class, that static member variable is effectively a static global object, and thus it will be constructed in no particular order.*
- In the case of our "foo" example, if the particular foo object is constructed before the set "all_of_me" is constructed, then foo's constructor will try to stick "this" into a set whose contents are probably entirely zero.
- If that doesn't crash then when the set is constructed (after the foo object) the set will be fully reinitialized, causing our object to be lost.
The beauty of intrinsic lists is: C++ guarantees it will do all of the static initialization (that is, zero-setting, etc.) before any functions or methods are called. So we can be sure our list head pointer is NULL, and that's all we need to get started.
One final note - as I define catagories for this post I see Chris has catagories for both "rants" and "IIS". I can't imagine that you'd ever want IIS without the rant tag.
* Okay, so there are some rules on construction order. Trust me, they won't help you do anything productive!
No comments:
Post a Comment