gcc是否对静态成员初始化时间有任何保证,特别是关于模板类?gcc中模板的非延迟静态成员初始化?
我想知道我是否能够很好地保证静态成员(PWrap_T<T>::p_s
)将在main()
之前初始化,当时类在多个编译单元中实例化。在main的开始处尝试手动触摸每个编译单元中的符号是不现实的,但我不清楚其他任何方法都可行。
我已经喜欢在不同的单位bar()
和总是得到期望的结果的方法测试,但我需要知道当/如果曾经GCC将抽出地毯出来,无论是可以预防的。
此外,在库加载完成之前,DSO中的所有静态成员都会被初始化吗?
#include <iostream>
#include <deque>
struct P;
inline std::deque<P *> &ps() { static std::deque<P *> d; return d; }
void dump();
struct P {
P(int id, char const *i) : id_(id), inf_(i) { ps().push_back(this); }
void doStuff() { std::cout << id_ << " (" << inf_ << ")" << std::endl; }
int const id_;
char const *const inf_;
};
template <class T>
struct PWrap_T { static P p_s; };
// *** Can I guarantee this is done before main()? ***
template <class T>
P PWrap_T<T>::p_s(T::id(), T::desc());
#define PP(ID, DESC, NAME) /* semicolon must follow! */ \
struct ppdef_##NAME { \
constexpr static int id() { return ID; } \
constexpr static char const *desc() { return DESC; } \
}; \
PWrap_T<ppdef_##NAME> const NAME
// In a compilation unit apart from the template/macro header.
void dump() {
std::cout << "[";
for (P *pp : ps()) { std::cout << " " << pp->id_ << ":" << pp->inf_; }
std::cout << " ]" << std::endl;
}
// In some compilation unit.
void bar(int cnt) {
for (int i = 0; i < cnt; ++i) {
PP(2, "description", pp);
pp.p_s.doStuff();
}
}
int main() {
dump();
PP(3, "another", pp2);
bar(5);
pp2.p_s.doStuff();
}
(C++ 11§3.6.2/ 4 - [basic.start.init] :)
它实现定义是否一个非本地的动态初始化具有静态存储持续时间的变量在main的第一条语句之前完成。 如果初始化被推迟到main的第一条语句之后的某个时间点,它应该在第一个odr-use(3.2)之前出现,在任何函数或变量中定义在同一个翻译单元中作为要初始化的变量。
...具有初始化带副作用的静态存储持续时间的非局部变量必须初始化,即使它没有使用odr(3.2,3.7.1)。
此外,试图__attribute__ ((init_priority(int)))
或__attribute__ ((constructor))
为模板成员的初始化产生warning: attributes after parenthesized initializer ignored
,我知道关于静态初始化没有其他招数。
在此先感谢任何能够给我答案的人!
我想象中的'ODR-use'规则意在包括可能有文件范围的对象的动态共享对象(的DSO)。如果DSO在主启动后被带入'dlopen()',那么显然不能初始化DSO中的所有内容,但理论上'dlopen()'可以在调用DSO中的其他任何内容之前确保DSO中的所有内容都已初始化。我想最终的答案是由ABI为您编译的任何操作系统/体系结构定义的。 –
单身模式解决了这个问题,不是吗? – lkanab