Is the order of static variables at the file level the same within a given translation unit?

I have a program broken into two source files:

example.cpp

#include <iostream>

class A {
 public:
   A(int x) {
      ::std::cout << "In A(" << x << ")\n";
   }
};

static A first(1);
static A second(2);

example__main.cpp

int main(int argc, const char *argv[])
{
   return 0;
}

Is the release of this program a guarantee:

In A(1)
In A(2)

on all platforms and compilers? If so, where in the standard does it say? Does it matter if I use namespaces and firstand secondappear in different namespaces? How about if they are not static and I use an anonymous namespace?

+5
source share
2 answers

, , .

++ 03,

(3.6/2) , , . [: 8.5.1 , . 6.7. ]

+5

:

  • -, , " ", (. ).

  • " " .

  • " ", "" ( ).

( - "", ). TU .

: " ", ( , constexpr), ​​static ( ): static int a; Foo b(1, true, Blue); , - "", non-constexpr, " ". , , ""; "" ..

( , ++ 11, .)

+3

All Articles