请求对“静态初始化命令Fiasco”的详细描述。 [英] Request for the detailed description of "Static Initialization Order Fiasco"

查看:114
本文介绍了请求对“静态初始化命令Fiasco”的详细描述。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在faq-lite中阅读了SIOF,但我真的不明白为什么会出现这个问题。我有一个静态库(.a),我使用该库使用其静态const数据成员对象类型。然后我使用静态const数据成员对象类型并分配给一个全局变量(对象)。但是当我使用全局变量到我的主或任何局部函数时,似乎全局变量是空的。我清楚地知道我的问题是SIOF,但我真的不明白为什么我的静态const数据成员对象没有初始化。

I read about the SIOF in the faq-lite and still I really don't understand why the issue happens. I have a static library(.a) and I use that library to use its static const data member object type. Then that static const data member object type I use and assign to a global variable(object). But it seems that global variable is empty when I use that global variable to my main or to any local function. I know obviously that my issue is SIOF but I really don't understand why my static const data member object was not initialized.

这是静态库,所以我想当我们创建我们的静态库时,静态const数据成员对象被编译并链接到该静态库,如果我错了,请纠正我..

It was static library so I guess when we created our static library the static const data member object was compiled and linked to that static library, correct me if I'm wrong..

//libsource.h
class foo
{
   public:
   ....

   public:
   static const barbar foofaa;
};

//libsource.cpp
const barbar foo::foofaa = barbar();

//main.cpp
#include <libsource.h>

barbar foos= foo::foofaa;

int main()
{
   //can't use foos because its empty
}

请咨询。为什么静态const数据成员对象没有初始化,即使它在静态库?

Please advice. Why that static const data member object was not initialized even if its in the static library?

非常感谢。

推荐答案

静态初始化顺序fiasco简单的:单个翻译单元中的静态对象以它们被声明的顺序被初始化,但是不能保证关于不同翻译单元中的静态对象相对于彼此被初始化的顺序。

The static initialization order fiasco is fairly straightforward: static objects in a single translation unit are initialized in the order in which they are declared, but there is no guarantee as to the order in which static objects in different translation units are initialized with respect to each other.

因此,在您的具体示例中, foos main.cpp 可以在 libsource.cpp 中声明的 foo :: foofaa 之前初始化

So, in your specific example, foos in main.cpp may be initialized before foo::foofaa, which is declared in libsource.cpp.

这篇关于请求对“静态初始化命令Fiasco”的详细描述。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆