Can I safely store things in a vector in constructors of non-pod static data member 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 defined behavior?
Note: The question is not about static locals, but about std::vector
depending on any static initialization that might not happen until the bar constructor.
Static variables inside a function or method are initialized on the first call. Thus the above snippet will work as intended.
Call Order:
std::vector
does not use any static data, so it is safe.
Even a strange implementation that did use static data in std::vector
would be required to ensure that its use of that data was safe and invisible to you (e.g. by using local statics instead of global objects), so you can assume it behaves as if it doesn't use any static data.
Inserting into a std::vector
does use the freestore (aka heap) which is global and uses static data, but that is safe to do from global constructors and is not specific to std::vector
but applies to any memory allocation done by global constructors before main
starts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With