Files
envi-code/SourceCode/Code2026/inc/singleton.h
T

26 lines
546 B
C++
Raw Normal View History

2026-10-04 00:04:20 +08:00
#pragma once
2026-09-28 15:28:50 +08:00
template <typename T>
struct Singleton
{
struct object_creator
{
object_creator(){ Singleton<T>::instance(); }
inline void do_nothing()const {}
};
static object_creator create_object;
public:
typedef T object_type;
static object_type& instance()
{
static object_type obj;
2026-10-04 00:04:20 +08:00
//据说这个do_nothing是确保create_object构造函数被调用
//这跟模板的编译有关
2026-09-28 15:28:50 +08:00
create_object.do_nothing();
return obj;
}
};
template <typename T> typename Singleton<T>::object_creator Singleton<T>::create_object;