gcc中模板的非延迟静态成员初始化?

Jef*_*eff 10 c++ gcc instantiation static-initialization

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();
}
Run Code Online (Sandbox Code Playgroud)

(C++11§3.6.2/ 4 - [basic.start.init] :)

实现定义了具有静态存储持续时间的非局部变量的动态初始化是否在main的第一个语句之前完成.如果初始化延迟到第一个main语句之后的某个时间点,则应该在与要初始化的变量相同的转换单元中定义的任何函数或变量的第一个odr-use(3.2)之前发生.

...具有静态存储持续时间且具有副作用初始化的非局部变量必须初始化,即使它没有使用(3.2,3.7.1).

此外,尝试__attribute__ ((init_priority(int)))__attribute__ ((constructor))模板成员的初始化产生warning: attributes after parenthesized initializer ignored,我知道没有其他关于静态初始化的技巧.

在此先感谢任何能给我答案的人!

Mar*_*ork 3

该标准保证在从外部源使用与对象相同的翻译单元中的任何函数/变量之前初始化静态存储持续时间对象。

这里的措辞旨在与共享库一起使用。由于共享库可以在 main() 启动后动态加载,因此语言规范必须足够灵活以应对它。但是,只要您从翻译单元外部访问您的对象,那么您就可以保证它会在您获得访问权限之前被构造出来(除非您正在做一些病态的事情)。

但是,如果它在同一编译单元中的另一个静态存储持续时间对象的构造函数中使用,这不会阻止它在初始化之前使用。

但是您可以通过使用以下技术轻松地手动保证静态对象在使用之前已正确初始化。

只需将静态变量更改为静态函数即可。然后在函数内部声明一个返回的静态成员。所以你可以像以前一样使用它(只需添加())。

template <class T>
struct PWrap_T
{
    static P& p_s();  // change static variable to static member function.
                      // Rather than the type being P make it a reference to P
                      // because the object will be held internally to the function
};

template <class T>
P& PWrap_T<T>::p_s()
{
    // Notice the member is static.
    // This means it will live longer than the function.
    // Also it will be initialized on first use.
    // and destructed when other static storage duration objects are destroyed.
    static P p_s_item(T::id(), T::desc());

    return p_s_item;

    // Note its not guaranteed to be created before main().
    // But it is guaranteed to be created before first use.
}
Run Code Online (Sandbox Code Playgroud)

因此,在这里您可以获得全局的好处(无论它们是什么)。您可以获得有保证的构造/销毁,并且您知道该对象在使用之前将被正确构造。

您需要做的唯一改变是:

void bar(int cnt) {
  for (int i = 0; i < cnt; ++i) {
    PP(2, "description", pp);
    pp.p_s().doStuff();
     //   ^^  Add the braces here.
  }
}
Run Code Online (Sandbox Code Playgroud)