#pragma once template struct Singleton { struct object_creator { object_creator(){ Singleton::instance(); } inline void do_nothing()const {} }; static object_creator create_object; public: typedef T object_type; static object_type& instance() { static object_type obj; //据说这个do_nothing是确保create_object构造函数被调用 //这跟模板的编译有关 create_object.do_nothing(); return obj; } }; template typename Singleton::object_creator Singleton::create_object;