源码见文章底部。
class singleton<T> 声明了一个静态引用 static T & m_instance;
这个静态引用是用来干嘛的呢?注意到该文件末尾有如下代码:
template<class T>
T & singleton< T >::m_instance = singleton< T >::get_instance();
读到这里应该大概能理解了,该引用只是为了使用static的特性,该全局对象在程序初始化之前就进行了实例化了对象。
单例函数get_instance()方位属性为private。内部有如下定义 class singleton_wrapper : public T {};将T的构造函数保护了起来。先将静态成员赋值然后在堆中构造对象,静态成员指向他。
is_destroyed()主要作用是对生命周期做控制管理。
而template <class T> static void use(T const *) 只是为了消除未初始化的警告,可参考另一工具类
boost::template <typename... Ts>void ignore_unused()的使用。最后调用构造生成实例对象并返回其引用。
// refer to instance, causing it to be instantiated (and
// initialized at startup on working compilers)
BOOST_ASSERT(! is_destroyed());
// note that the fo