1

I'm very well aware of the general static constructor order problem, however I'm curious to know if this also applies in the case of composition.

Say I have the following:

#include <vector>
using std::vector;

class Foo
{
    static vector<int> myVector;
    Foo(int i) { myVector.push_back(i); }
    ~Foo() {}
};

vector<int> Foo::myVector;
Foo foo_1(1);
Foo foo_2(2);

do I have any assurances that myVector's ctor will be called before either of the calls to Foo's ctor?

Needless to say, testing this and finding that it does work as hoped proves nothing.

dgnuff
  • 3,195
  • 2
  • 18
  • 32
  • Within the same TU, static initialization is in fact generally ordered (template instantiations being an exception). The problem is when you have different TUs. – T.C. Nov 21 '15 at 22:28
  • The order is well-defined _inside_ a translation unit – sehe Nov 21 '15 at 22:28
  • @vsoftco please cite the question this is a duplicate of, because I searched extensively and could not find anything that specifically addresses this specific issue, in particular whether composition has any effect. Thanks. – dgnuff Nov 21 '15 at 22:48
  • @dgnuff *The storage for objects with static storage duration (basic.stc.static) shall be zero-initialized (dcl.init) before any other initialization takes place*. This is the first sentence in the accepted answer. I think it implicitly implies that the ctor is used later. The composition has nothing to do with it, the usual rules are followed, i.e. the members are constructed *before* the object that contains them. – vsoftco Nov 21 '15 at 22:49
  • @vsoftco Rather more relevant is this: "The other guaranteed thing is that initialization of static objects from a translation unit will be done before use of any object or function from this translation unit:" which implies that myVector must be initialized before it is used *in any TU*. – dgnuff Nov 21 '15 at 22:57
  • Ok, will reopen your question. Here is maybe a better dupe: http://stackoverflow.com/questions/16873215/constructor-calling-order-with-composition But again, your question is really not about `static`, but about the order of initialization of composite objects: *do I have any assurances that myVector's ctor will be called before either of the calls to Foo's ctor?* – vsoftco Nov 21 '15 at 22:57
  • The "double static" nature of this problem does differentiate it from all other cases, which are trivially well defined. If myVector is non-static, then it's clearly constructed during the construction of any Foo, static or not. Likewise, if myVector remains a static member, and I define a non-static Foo, then also trivially myVector will be initialized first since it's static. – dgnuff Nov 21 '15 at 23:07

1 Answers1

1

The order is undefined across different compilation units. Within the same compilation unit the order is well defined: The same order as definition.

Thus, your vector is going to be initialized before foo_1 and foo_2.

101010
  • 41,839
  • 11
  • 94
  • 168