/** 单例模式采用boost源码 由于boost单例返回的是引用,下面改成指针 */ template<classT> classBaseSingleton { public: BaseSingleton(){} private: structobject_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() { BaseSingleton<T>::Instance(); } inlinevoiddo_nothing()const{ } }; static object_creator create_object;
//BaseSingleton();
public: typedef T object_type;
// If, at any point (in user code), BaseSingleton<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 BaseSingleton<T>::create_object, whose constructor is // called before main() begins. create_object.do_nothing();