Are all static variables problematic?

Hi,

Static variables get instantiated at load time or shortly after and at a non deterministic order. This makes me think that using such variables is inherently problematic. Is this so, for instance looking at the following code?

1
2
3
4
5
6
struct Comparison
{
    static std::locale loc;
    static std::ostringstream os;
    ///////.....        
}


When should we avoid static variables and when can we use them without danger?

Thanks,

Juan
There is plenty of determinism in the order they are loaded. For your example, the "ordered dynamic initialization" rule applies, to quote http://en.cppreference.com/w/cpp/language/initialization

Ordered dynamic initialization, which applies to all other non-local variables: within a single translation unit, initialization of these variables is always sequenced in exact order their definitions appear in the source code

The problems with statics are hidden dependencies and potential for data races (e.g. if in future you parallelize some code which calls some other code which uses Comparison::os)
Last edited on
Thanks for pointing me to that document. There are 2 points I do not understand in there:

1) Unordered dynamic initialization, which applies only to (static/thread-local) class template data members that aren't explicitly specialized. Initialization of such static variables is indeterminately sequenced with respect to all other dynamic initialization except if the program starts a thread before a variable is initialized, in which case its initialization is unsequenced (since C++17). Initialization of such thread-local variables is unsequenced with respect to all other dynamic initialization.
2) Partially-ordered dynamic initialization, which applies to all inline variables that are not an implicitly or explicitly instantiated specialization. If a partially-ordered V is defined before ordered or partially-ordered W in every translation unit, the initialization of V is sequenced before the initialization of W (or happens-before, if the program starts a thread)

Doesn't these cases prove that initialization is NOT always deterministic?
I am not sure what is meant by "(static/thread-local) class template data members that aren't explicitly specialized", could you please elaborate?

Thanks
Juan
Some things are deterministic, some other things aren't. I was objecting to "Static variables get instantiated at a non deterministic order." since that makes it sound like nothing is certain.

I am not sure what is meant by "(static/thread-local) class template data members that aren't explicitly specialized", could you please elaborate?
1
2
3
4
5
6
7
8
9
// This is a class template with a static data member:
template<class T>
struct S1 { static T n1; };
// This is its definition
template<class T> T S1<T>::n1 = {};
// This is an explicit speciaization
template<> std::string S1<std::string>::n1 = "abc";
// now, S1<string>::n1 uses ordered init
// but S1<vector<int>>::n1 uses unordered init 

Last edited on
Thanks!!
Topic archived. No new replies allowed.