#pragma once #include #include #include // for std::forward template class Singleton { public: template static T* GetInstance(Args&&... args) { if (!m_instance) { std::call_once(m_onceFlag, [] { m_instance.reset(new T(std::forward(args)...)); }); } return m_instance.get(); } protected: Singleton() = default; virtual ~Singleton() = default; private: Singleton(const Singleton&) = delete; Singleton(Singleton&&) = delete; Singleton& operator=(const Singleton&) = delete; Singleton& operator=(Singleton&&) = delete; private: static std::unique_ptr m_instance; static std::once_flag m_onceFlag; }; template std::unique_ptr Singleton::m_instance; template std::once_flag Singleton::m_onceFlag;