C++可继承的单例基类模板

一、介绍

最近在写一个项目,其中用到好几个单例,类本身的设计不是很复杂,但是如果每个都写一遍单例又觉得有点冗余;所以查资料写了一个单例基类模板,只要一个类继承这个基类并且以自身作为模板参数就可以实现一个单例;关于单例本身的介绍在这里不重点介绍,感兴趣的可以看一下我的另外一篇博文(https://www.cnblogs.com/sunchaothu/p/10389842.html),写的非常详细,把各种单例实现方法总结了一遍,并且给出技术细节分析。

特点:

  • 线程安全,使用 staic local,保证线程安全 (C++11)
  • 可以继承的模板,更加方便的实现单例

二、代码

// author: sunchaothu
// brief: a singleton base class offering an easy way to create singleton
#include <iostream>

template<typename T>
class Singleton{
public:
    static T& get_instance() noexcept(std::is_nothrow_constructible<T>::value){
        static T instance;
        return instance;
    }
    virtual ~Singleton() noexcept{
        std::cout<<"destructor called!"<<std::endl;
    }
    Singleton(const Singleton&)=delete;
    Singleton& operator =(const Singleton&)=delete;
protected:
    Singleton(){
        std::cout<<"constructor called!"<<std::endl;
    }

};
/********************************************/
// Example:
// 1.friend class declaration is requiered!
// 2.constructor should be private


class DerivedSingle:public Singleton<DerivedSingle>{
   // !!!! attention!!!
   // needs to be friend in order to
   // access the private constructor/destructor
   friend class Singleton<DerivedSingle>;
public:
   DerivedSingle(const DerivedSingle&)=delete;
   DerivedSingle& operator =(const DerivedSingle&)= delete;
private:
   DerivedSingle()=default;
};

int main(int argc, char* argv[]){
    DerivedSingle& instance1 = DerivedSingle::get_instance();
    DerivedSingle& instance2 = DerivedSingle::get_instance();
    return 0;
}

以上代码放在了 我的github中: https://github.com/sunchaothu/AdvancedCpp/tree/master/singleton

三、关键处

  • 子类需要把自己作为模板参数,如 class DerivedSingle:public Singleton<DerivedSingle>;
    这里用到的是CRTP(Curiously recurring template pattern) 奇异循环模板模式
  • 在子类中需要把基类 Singleton 声明为友元;这样才能访问私有构造函数(这个链接里还提供了不需要声明为友元的实现)
  • 这个使用了局部静态变量保证了线程安全,成为 Magic Static。

四、使用限制

继承了这个类的代码不可以作为基类再被继承。

五、参考资料

以上代码参考了以下链接;如果有纰漏之处,还请指正,以免误人子弟!

  1. https://stackoverflow.com/questions/34519073/inherit-singleton?r=SearchResults
  2. https://www.cnblogs.com/lidabo/p/3949226.html
原文地址:https://www.cnblogs.com/sunchaothu/p/10353507.html