#pragma once #include#include template class MySingleton { public: static std::shared_ptr getInstance(); private: static std::shared_ptr m_instance ; MySingleton(); }; template std::shared_ptr MySingleton ::m_instance{ nullptr }; template std::shared_ptr MySingleton ::getInstance() { static std::once_flag oc;//用于call_once的局部静态变量 std::call_once(oc, [&] { MySingleton ::m_instance = std::make_shared (); }); return m_instance; }
支持多线程
可以在VS2022下通过
使用:
如有错误和更好的方法,请不吝指正。



