template<typename T>
class Singleton
{
public:
Singleton() = default;
~Singleton() = default;
//forbid copy and asign
Singleton(const Singleton &) = delete;
Singleton&operator=(const Singleton&) = delete;
//make singleton instance by args
template <typename...Args>
static void makeInstance(Args&&...args)
{
std::call_once(flag, make_shared_instance<T>, std::forward<Args>(args)...);
}
//get instance
static std::shared_ptr<T> Instance()
{
if (!instance)
{
throw std::exception("instance not make!");
}
return instance;
}
private:
template <typename...Args>
static void make_shared_instance(Args&&...args)
{
instance = std::make_shared<T>(std::forward<Args>(args)...);
}
static std::once_flag flag;
static std::shared_ptr<T> instance;
};
when I use it like this : Singleton::makeInstance(10); which with parameters it works well but
Singleton::makeInstance(); which without parameters it works not well. this is why? should one help me ?
Aucun commentaire:
Enregistrer un commentaire