这是旧主题的另一个变体:不同翻译单元中静态对象的初始化顺序没有定义。
下面是我的特定场景的精简示例。G 和 F 类是非 POD 类型。F 依赖于 G 是指要构造 F 的实例,您需要一定数量的 G 实例。(例如,F 可能是应用程序发出的某些消息,而 G 的实例将是此类消息的组件。)
G.hpp
#ifndef G_HPP
#define G_HPP
struct G
{
G() {} // ...
};
inline G operator+(G, G) { return G(); }
#endif
GS.hpp
#ifndef GS_HPP
#define GS_HPP
#include "G.hpp"
extern const G g1;
extern const G g2;
extern const G g3;
extern const G g4;
extern const G g5;
extern const G g6;
extern const G g7;
extern const G g8;
extern const G g9;
#endif
GS.cpp
#include "Gs.hpp"
const G g1;
const G g2;
const G g3;
const G g4;
const G g5;
const G g6;
const G g7;
const G g8;
const G g9;
F.hpp
#ifndef F_HPP
#define F_HPP
#include "G.hpp"
struct F
{
F(G) {} // ...
};
#endif
文件系统
#ifndef FS_HPP
#define FS_HPP
#include "F.hpp"
extern const F f1;
extern const F f2;
extern const F f3;
#endif
fs.cpp
#include "Fs.hpp"
#include "Gs.hpp"
const F f1(g1 + g2 + g3);
const F f2(g4 + g5 + g6);
const F f3(g7 + g8 + g9);
F 的构造函数接受一个参数,该参数是应用于 G 的实例的结果
operator+
。由于 F 和 G 的实例都是全局变量,因此不能保证 G 的实例在 F 的构造函数需要它们时已经被初始化。
这里的特殊性是到处都有很多 G 和 F,我想尽可能地保持语法接近上面发布的代码,同时仍然在 F 需要时强制构造 G。