0

My recollection is that ordered initialization is only guaranteed within a translation unit. There is no guarantee that any particular translation unit has been initialized before any other, so the following was dangerous and undefined:

myClass global_var();

int main() {
  return 0;
}

I'm curious about initialization lists though. What if I were to do the following:

std::array<MyClass,2> global_array = {
  MyClass(),
  MyClass()
};

int main() {
  return 0;
}

Is the initializer list guaranteed to run after the initialization of all translation units?

Barry
  • 286,269
  • 29
  • 621
  • 977
Fred
  • 3,786
  • 7
  • 41
  • 52
  • 5
    Do you really mean the `()` in `global_var`? That makes `global_var` a *function* (compare `myClass global_var();` and `int foo();`) – Angew is no longer proud of SO Nov 12 '15 at 14:06
  • Look [here](http://stackoverflow.com/a/3553682/3410935) – Glapa Nov 12 '15 at 15:09
  • Possible duplicate of [How are local and global variables initialized by default?](http://stackoverflow.com/questions/3553559/how-are-local-and-global-variables-initialized-by-default) – jaredready Nov 12 '15 at 15:12
  • What do you mean by "initialization of a translation unit", exactly? What preconditions do you believe must hold for `global_array` definition you show to be valid? What do you feel can go wrong with it? I don't at all understand the nature of your concern. You start with the issue of undefined order of initialization of global variables between different translation units - but your examples only consist of one translation unit each. What problem are they supposed to demonstrate? – Igor Tandetnik Nov 12 '15 at 21:30

0 Answers0