为什么C++单例实例化需要do_nothing方法



我正在查看http://www.boost.org/doc/libs/1_47_0/boost/pool/detail/singleton.hpp

我的问题是:由于create_object是singleton_default类的静态成员,所以应该在main之前调用它的构造函数。从object_creator的构造函数中,调用singleton_default::instance,确保obj在main之前实例化。我不明白的是需要do_nothing方法。文档提到它强制实例化create_object,但不是应该在main启动之前初始化类的静态成员吗?那么,singleton_default::create_object实例化是否还不够好?

这是代码

// T must be: no-throw default constructible and no-throw destructible
template <typename T>
struct singleton_default
{
  private:
    struct object_creator
    {
      // This constructor does nothing more than ensure that instance()
      //  is called before main() begins, thus creating the static
      //  T object before multithreading race issues can come up.
      object_creator() { singleton_default<T>::instance(); }
      inline void do_nothing() const { }
    };
    static object_creator create_object;
    singleton_default();
  public:
    typedef T object_type;
    // If, at any point (in user code), singleton_default<T>::instance()
    //  is called, then the following function is instantiated.
    static object_type & instance()
    {
      // This is the object that we return a reference to.
      // It is guaranteed to be created before main() begins because of
      //  the next line.
      static object_type obj;
      // The following line does nothing else than force the instantiation
      //  of singleton_default<T>::create_object, whose constructor is
      //  called before main() begins.
      create_object.do_nothing();
      return obj;
    }
};
template <typename T>
typename singleton_default<T>::object_creator
singleton_default<T>::create_object;

我尝试删除do_nothing方法,但在main之前停止了对象实例化。

您必须查看3.6.2节中的非局部变量初始化的标准

首先是第2点的原则:

具有静态存储持续时间(…)的变量应初始化为零在进行任何其他初始化之前。

执行恒定初始化:(…)

零初始化和常量初始化统称为静态初始化;所有其他初始化都是动态的初始化。在任何进行动态初始化。动态初始化具有静态存储持续时间的非局部变量是有序的或无序的。显式专用类模板静态的定义数据成员已下令初始化。

然后在第4点中,对您的问题的解释(您的单例需要"动态初始化"):

它是由实现定义的具有静态存储持续时间的非局部变量在主语句的第一个语句。如果初始化推迟到在main的第一个语句之后的时间点,它应发生在在odr中定义的任何函数或变量的第一次使用转换单元作为要初始化的变量。

这个do_nothing()只是确保了第一次使用和动态初始化的顺序。

如果没有do_nothing(),全局静态create_object()就不需要在第一次调用instance()之前初始化,而该函数中的静态obj只会在第一次呼叫时初始化,即在main()启动之后。

最新更新