C++11 Improved design mode singleton mode

Source: Internet
Author: User

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&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.