Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat are the rules regarding initialization of non-local statics?
    primarykey
    data
    text
    <p>Suppose I have a class whose only purpose is the side-effects caused during construction of its objects (e.g., registering a class with a factory):</p> <pre><code>class SideEffectCauser { public: SideEffectCauser() { /* code causing side-effects */ } }; </code></pre> <p>Also suppose I'd like to have an object create such side-effects once for each of several translation units. For each such translation unit, I'd like to be able to just put an a <code>SideEffectCauser</code> object at namespace scope in the .cpp file, e.g.,</p> <pre><code>SideEffectCauser dummyGlobal; </code></pre> <p>but 3.6.2/3 of the C++03 standard suggests that this object need not be constructed at all unless an object or function in the .cpp file is used, and articles such as <a href="http://accu.org/index.php/journals/597" rel="nofollow noreferrer">this</a> and online discussions such as <a href="http://www.gamedev.net/topic/612598-self-registering-factory-in-c/" rel="nofollow noreferrer">this</a> suggest that such objects are sometimes not initialized. </p> <p>On the other hand, <a href="https://stackoverflow.com/questions/582331/is-there-a-way-to-instantiate-objects-from-a-string-holding-their-class-name/582456#582456">Is there a way to instantiate objects from a string holding their class name? </a> has a solution that is claimed to work, and I note that it's based on using an object of a type like <code>SideEffectCauser</code> as a static data member, not as a global, e.g., </p> <pre><code>class Holder { static SideEffectHolder dummyInClass; }; SideEffectHolder Holder::dummyInClass; </code></pre> <p>Both <code>dummyGlobal</code> and <code>dummyInClass</code> are non-local statics, but a closer look at 3.6.2/3 of the C++03 standard shows that that passage applies only to objects at namespace scope. I can't actually find anything in the C++03 standard that says when non-local statics at class scope are dynamically initialized, though 9.4.2/7 suggests that the same rules apply to them as to non-local statics at namespace scope.</p> <p>Question 1: In C++03, is there any reason to believe that <code>dummyInClass</code> is any more likely to be initialized than <code>dummyGlobal</code>? Or may both go uninitialized if no functions or objects in the same translation unit are used?</p> <p>Question 2: Does anything change in C++11? The wording in 3.6.2 and 9.4.2 is not the same as the C++03 versions, but, from what I can tell, there is no behavioral difference specified for the scenarios I describe above.</p> <p>Question 3: Is there a reliable way to use objects of a class like <code>SideEffectHolder</code> outside a function body to force side-effects to take place?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload