【问题标题】:Why is the do_nothing method needed for C++ singleton instantiation?为什么 C++ 单例实例化需要 do_nothing 方法?
【发布时间】:2014-11-02 16:19:10
【问题描述】:

我正在查看来自 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 之前停止对象实例化。

【问题讨论】:

    标签: c++ design-patterns boost


    【解决方案1】:

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

    首先是第2点的原则:

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

    执行不断的初始化:(...)

    一起调用零初始化和常量初始化 静态初始化;所有其他初始化都是动态的 初始化。静态初始化应在任何之前执行 动态初始化发生。动态初始化 具有静态存储持续时间的非局部变量是有序的或 无序的。显式特化类模板静态的定义 数据成员已排序初始化。

    然后在第 4 点,对您的问题的解释(您的单身人士需要“动态初始化”):

    是否动态初始化是实现定义的 具有静态存储持续时间的非局部变量在 main的第一个语句。如果初始化推迟到某些 在 main 的第一个语句之后的时间点,它应该发生在之前 任何在同一文件中定义的函数或变量的首次 odr 使用 翻译单元作为要初始化的变量。

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

    你不会有do_nothing(),在你第一次调用instance()之前不需要初始化全局静态create_object(),并且这个函数内部的静态obj只会在第一次调用,即在main() 开始之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多