According to the standard, does std :: vector affect static order initialization problems?

Can I safely store things in a vector in non-pod static data element constructor constructors? Example:

class Foo
{
public:
    static Foo& instance()
    {
        static Foo inst;
        return inst;
    }

    void store(int x) { numbers.push_back(x); }

private:
    Foo() {}
    std::vector<int> numbers;
};

class Bar
{
public: 
    Bar() { Foo::instance().store(5); }
};

class Thing
{
public:
    static Bar bar;
};

// in thing.cpp:
Bar Thing::bar;

Is the above code correct and produces specific behavior?

Note. The question is not about static locales, but about std::vectordepending on any static initialization that may not occur before the bar constructor.

+4
source share
2 answers

std::vector does not use any static data, therefore it is safe.

, std::vector, , (, ), , .

std::vector freestore (aka heap), , , std::vector, , main.

+5

. , .

:

  • Foo:: ()
  • Foo ( )
+6

Source: https://habr.com/ru/post/1530303/


All Articles