为什么需要C ++单例的do_nothing方法? [英] Why is the do_nothing method needed for C++ singleton instantiation?

查看:1231
本文介绍了为什么需要C ++单例的do_nothing方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要的文件在从的http:/ /www.boost.org/doc/libs/1_47_0/boost/pool/detail/singleton.hpp

我的问题:既然create_object是类的静态成员singleton_default它的构造函数之前应该主要被调用。从object_creator的构造,singleton_default ::实例调用,它可以确保将obj前主实例化。我不遵循对于do_nothing方法的必要性。该文件提到它迫使create_object的实例,但不应该类的静态成员主要开始前被初始化?通过该令牌应该singleton_default :: create_object实例不够好?

My question: since create_object is a static member of class singleton_default its constructor should be called before main. From the constructor of object_creator, singleton_default::instance is called, which makes sure obj is instantiated before main. What I don't follow is the need for the do_nothing method. The documentation mentions it forces the instantiation of create_object but are not static members of class supposed to be initialized before main starts? By that token should singleton_default::create_object instantiation not be good enough?

这里的code

// 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方法,但在此之前主要是停止对象实例化。

I tried removing the do_nothing method, but that stopped the object instantiation before main.

推荐答案

您不得不看的标准部分的 3.6.2初始化的非本地变量

You have to look at the standard in section 3.6.2 initialisation of non local variables.

首先,在2点的原则是:

First the principle in point 2:

与静态存储时间(...)变量应被零初始化
  之前的任何其他初始化发生。

Variables with static storage duration (...) shall be zero-initialized before any other initialization takes place.

恒初始化:(...)

一起,零初始化和恒定初始化被称为
  静态初始化;所有其他的初始化动态
  初始化。静态初始化应前的任何执行
  动态初始化发生。的动态初始化
  与静态存储持续时间的非本地变量或者是订购或
  无序的。明确专门的模板类静态的定义
  数据成员已下令初始化。

Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place. Dynamic initialization of a non-local variable with static storage duration is either ordered or unordered. Definitions of explicitly specialized class template static data members have ordered initialization.

然后在4点,你的问题的解释(你单身,需要一个动态intialisation):

Then in point 4, the explanation for your question (your singleton requiring a "dynamic intialisation") :

这是实现定义是否一个动态初始化
  与静态存储持续时间的非局部变量前完成
  主要的第一个语句。如果初始化被推迟到一些
  时间点主要在第一条语句后,应发生前
  第一ODR使用在同一个定义的任何函数或变量
  翻译单元作为变量被初始化。

It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first odr-use of any function or variable defined in the same translation unit as the variable to be initialized.

do_nothing()只是确保本首次使用和动态初始化的顺序。

This do_nothing() just ensures this first use and the order of the dynamic initialisation.

你会不会有 do_nothing(),全局静态 create_object()就没有必要了)的首次呼叫实例(,和静态 OBJ 这个函数内只在初始化之前initalized第一个电话,开始后,即主()

Wouldn't you have the do_nothing(), the global static create_object() wouldn't be needed to be initalized before your first call of instance(), and the static obj inside this function would only be initialized at first call, i.e. after the start of main().

这篇关于为什么需要C ++单例的do_nothing方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆