开发者

c++: initializing static string members

开发者 https://www.devze.com 2023-04-06 06:13 出处:网络
I\'m having some problems initializing static string members in c++. I have several classes and each one is holding several static string members that represent an id. When I am initializing the varia

I'm having some problems initializing static string members in c++. I have several classes and each one is holding several static string members that represent an id. When I am initializing the variables by calling a static function everything is fine. However, when I'd like to assign one variable with the value of another it still holds empty string. What's problem with this code?

std::string A::id()
{
    std::stringstream sst; 
    sst << "id" << i;           
    i++;
    return sst.str();
}

  std::开发者_如何学JAVAstring B::str = A::id(); //prints "id0";
  std::string C::str = "str"; //prints str


  std::string D::str = B::str; //prints "" <-- what's wrong here?
  std::string D::str2 = C::str; //prints ""

It appears as if the variables I am referring to (B::str and C::str) havent been initialized yet. But I assume when D::str = B::str is executed C::str is initialized at the latest and therefore D::str should also hold the string "id0".


This is Static Initialization Fiasco.

As per the C++ Standard the initialization order of objects with static storage duration is unspecified if they are declared in different Translation units.

Hence any code that relies on the order of initialization of such objects is bound to fail, and this problem is famously known as The Static Initialization Fiasco in C++.

Your code relies on the condition that Initialization of B::str and C::str happens before D::str, which is not guaranteed by the Standard. Since these 3 static storage duration objects reside in different translation units they might be initialized in Any order.

How to avoid it?

The solution is to use Construct On First Use Idiom,in short it means replacing the global object, with a global function, that returns the object by reference. The object returned by reference should be local static, Since static local objects are constructed the first time control flows over their declaration, the object will be created only on the first call and on every subsequent call the same object will be returned, thus simulating the behavior you need.


This should be an Interesting read:

How do I prevent the "static initialization order fiasco"?



There is no guaranteed order of initialization for static variables. So don't rely on it. Instead init them with actual literal's, or better yet, init them at run-time when they are really needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号