Boost中的单例如何实现在调用main之前初始化所有单例? [英] How singleton in boost implement all singletons are initialized before main is called?

查看:143
本文介绍了Boost中的单例如何实现在调用main之前初始化所有单例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boost单例的源代码为 ,我不理解下面的源文件中的两种符号:

The source code of singleton of boost is there ,I don't understand two notations in the source file below:

// ***include this to provoke instantiation at pre-execution time***
static void use(T const &) {};

BOOST_DLLEXPORT static T & get_instance() {
static detail::singleton_wrapper< T > t;
***// refer to instance, causing it to be instantiated (and
// initialized at startup on working compilers)***
BOOST_ASSERT(! detail::singleton_wrapper< T >::m_is_destroyed);
use(instance);
return static_cast<T &>(t);
}

问题是:这段代码如何在main()之前强制c ++中的单例初始化?这两种表示法是什么意思?

Question is: How could this code force initialization of singleton in c++ before main()?What do these two notation mean?

推荐答案

不能.正是这一行:

template<class T>
BOOST_DLLEXPORT T & singleton< T >::instance = singleton< T >::get_instance();

它创建一个静态对象,该对象通过调用get_instance进行初始化.由于它是一个类静态对象,因此会在main之前进行初始化.

It creates a static object that is initialized by a call to get_instance. Since it's a class-static object, it's initialized before main.

这篇关于Boost中的单例如何实现在调用main之前初始化所有单例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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