For a code note on learning to apply c++11 in depth:
That's what c++11 did before it was done.
Template<typename t>class singleton{public:static t* Instance () {if (m_pinstance = = nullptr) m_pinstance = new T (); return m_pinstance;} Template<typename t0>static t* Instance (T0 arg0) {if (m_pinstance = = nullptr) m_pinstance = new T (arg0); return m_pins tance;} Template<typename t0,typename t1>static t* Instance (T0 arg0, T1 arg1) {if (m_pinstance = = nullptr) m_pinstance = new T (arg0, arg1); return m_pinstance;} Template<typename T0, TypeName t1,typename t2>static t* Instance (T0 arg0, T1 arg1,t2 arg2) {if (m_pinstance = = Nullpt r) m_pinstance = new T (arg0, ARG1,ARG2); return m_pinstance;} Template<typename T0, TypeName T1, TypeName T2,typename t3>static t* Instance (T0 arg0, T1 arg1, T2 arg2,t3 arg3) {if (m_pinstance = = nullptr) m_pinstance = new T (arg0, arg1, ARG2,ARG3); return m_pinstance;} Template<typename T0, TypeName T1, TypeName T2, TypeName T3,typename t4>static t* Instance (T0 arg0, T1 arg1, T2 arg2 , T3 arg3,t4 Arg4) {if (m_pinstance = = nullptr) m_pinstance = new T (arg0, Arg1, arg2, ARG3,ARG4); return m_pinstance;} Template<typename T0, TypeName T1, TypeName T2, TypeName T3, TypeName T4,typename t5>static t* Instance (T0 arg0, T1 Arg1, T2 arg2, T3 arg3, T4 arg4,t5 arg5) {if (m_pinstance = = nullptr) m_pinstance = new T (arg0, Arg1, arg2, Arg3, ARG4,ARG5) ; return m_pinstance;} Static t* getinstance () {if (m_pinstance = = nullptr) throw Std::logic_error ("the instance is not init,please init the Instan CE first "); return m_pinstance;} static void Destroyinstance () {Delete m_pinstance;m_pinstance = nullptr;} Private:singleton (void); virtual ~singleton (void); Singleton (const singleton&); singleton& operator = (const Singleton); static t* m_pinstance;}; Template<class t> t* singleton<t>::m_pinstance = nullptr;//============================================ struct A{a () {}};struct b{b (int x) {}};struct c{c (int x, double y) {}};int _tmain (int argc, _tchar* argv[]) {SINGLETON<A&G T;::instance (); Singleton<a>::instance (); Singleton<b>::instance (1); Singleton<c>::instance (1,3.14); Singleton<a>::D estroyinstance (); Singleton<b>::D estroyinstance (); Singleton<c>::D estroyinstance (); return 0;}
C++11 can be abbreviated after the use of variable template parameters
Template<typename t>class singleton{public:template <typename ... Args>static t* Instance (args&& ... Args) {if (m_pinstance = = nullptr) m_pinstance = new T (std::forward<args& gt; (args) ...); return m_pinstance;} Static t* getinstance () {if (m_pinstance = = nullptr) throw Std::logic_error ("the instance is not init,please initialize the Instance first "); return m_pinstance;} static void Destroyinstance () {Delete m_pinstance;m_pinstance = nullptr;} Private:singleton (void); virtual ~singleton (void); Singleton (const singleton&); singleton& operator= (const singleton&);p rivate:static t* m_pinstance;}; Template<class t>t* singleton<t>::m_pinstance = nullptr; #include <iostream> #include <string> Using namespace Std;struct a{a (const string&) {cout << "lvalue" << Endl;} A (string&&x) {cout << "rvalue" << Endl;}}; struct B{b (const string&) {cout << "lvalue" << Endl;} B (string&& x) {cout << "RvaluE "<< Endl; }};struct c{c (int x, double y) {}void fun () {cout << "Test" << Endl;}}; int _tmain (int argc, _tchar* argv[]) {string str = "BB"; Singleton<a>::instance (str); Singleton<b>::instance (Std::move (str)); Singleton<c>::instance (1,3.14); Singleton<c>::getinstance ()->fun (); Singleton<a>::D estroyinstance (); Singleton<b>::D estroyinstance (); Singleton<c>::D estroyinstance (); return 0;}
C++11 Improved design mode singleton mode