gpt4 book ai didi

c++ - Meyers Singletons 的销毁顺序

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:41 25 4
gpt4 key购买 nike

到以下代码:

class C {
public:
static C& Instance() {
static C c;
return c;
}

~C(){std::cout << "c destructed\n";}
private:
C(){}
};

class D{//similar to C but prints `d destructed` on destruction
//...

int main()
{
auto c = C::Instance();
auto d = D::Instance();
}
//outputs (with gcc)
//d destructed
//c destructed
//d destructed
//c destructed

我有几个问题:

  1. 销毁调用的顺序是否定义明确? (即使类 C 和 D 定义在不同的源文件中)
  2. 如果定义明确,此行为是否可移植?

最佳答案

这个构造的要点是强加一个构造顺序(因此也是一个销毁顺序)。

建筑

由于这些是局部静态变量,构造顺序由它们各自的Instance函数第一次被调用的顺序决定。

因为这是在 main 中完成的,所以构建顺序是完全指定的。

使顺序未指定的唯一方法是在不同翻译单元的静态初始化中使用它们,例如,如果有

C& the_c = C::Instance();

另一个有

D& the_d = D::Instance();

破坏

静态存储对象的销毁顺序与构造顺序相反。

3.6.3,终止,第 1 段:

If the completion of the constructor or dynamic initialization of an object with static storage duration is sequenced before that of another, the completion of the destructor of the second is sequenced before the initiation of the destructor of the first.

所以销毁顺序完全由构造顺序指定。

请注意,即使其中一个依赖于另一个,无论翻译单元如何,此单例构造都已明确指定。

也就是说,这是绝对安全的,无论它在哪里定义都无关紧要:

class C {
public:
static C& Instance() {
static C c(D::Instance());
return c;
}

~C(){ m_d.doSomething(); } // Yes, this is safe.
private:
C(D& d) : m_d(d) { m_d.doSomething(); } // Yes, this is safe.
D& m_d;
};

关于c++ - Meyers Singletons 的销毁顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40242063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com