Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 508 Bytes

File metadata and controls

29 lines (24 loc) · 508 Bytes
  • 单例模式保证一个类只有一个实例
template <typename T>
class Singleton {
 public:
  static T& Instance();
  Singleton(const Singleton&) = delete;
  Singleton& operator=(const Singleton&) = delete;

 private:
  Singleton() = default;
  ~Singleton() = default;
};

template <typename T>
T& Singleton<T>::Instance() {
  static T instance;
  return instance;
}

class A {};

int main() {
  auto& a = Singleton<A>::Instance();
  auto& b = Singleton<A>::Instance();
  assert(&a == &b);
}