带有重置选项的基本单例课
我正在尝试使用a 单线环境中的CRTP实现基类单
#include <type_traits>
#include <atomic>
#include <cassert>
//Single threaded singleton.
//T is default constructable.
template<typename T>
class singleton
{
static bool m_empty;
public:
virtual ~singleton()
{
this->reset();
}
//YOU will end up with nullptr if I reset: YES THAT IS WHAT I WANT, YOU SHOULDN'T HAVE THE POINTER AT FIRST PLACE.
//Calls should be done this way: singleton::instance()->do_stuff(); Never hold the instance ++ Single threaded.
static T* instance()
{
static T* pme = new T();
if (pme)
return pme;
m_empty = false;
pme = new T();
return pme;
};
static bool reset()
{
if(m_empty)
return false;
T* pme = instance();
m_empty = true;
delete pme;
return true;
};
protected:
singleton() = default;
};
顿研究员:
class derive : public singleton<derive>
{
public:
derive() = default;
double get() const
{
return m_example;
}
private:
double m_example;
};
Q1 。可以吗? (请参阅上面的限制。我知道这在多线程环境中以及是否持有实例无效)。
Q2 。这样做时我会遇到错误:
int main(int argc, char* argv[])
{
const double d = derive::instance()->get();
};
>main.cpp
1>main.obj : error LNK2001: unresolved external symbol "private: static bool singleton<class derive>::m_empty" (?m_empty@?$singleton@0_NA)
1>..\build\bin\Debug\windows\x86_64\tests\tests.exe : fatal error LNK1120: 1 unresolved externals
您能帮我吗?
Q3 。我不需要这个,但是有什么方法可以在多线程Env中使用CRTP class Singleton拥有一个基础?
I am trying to implement a base class singleton using CRTP in a single threaded environnement where the instance is not held by the user, code is below:
#include <type_traits>
#include <atomic>
#include <cassert>
//Single threaded singleton.
//T is default constructable.
template<typename T>
class singleton
{
static bool m_empty;
public:
virtual ~singleton()
{
this->reset();
}
//YOU will end up with nullptr if I reset: YES THAT IS WHAT I WANT, YOU SHOULDN'T HAVE THE POINTER AT FIRST PLACE.
//Calls should be done this way: singleton::instance()->do_stuff(); Never hold the instance ++ Single threaded.
static T* instance()
{
static T* pme = new T();
if (pme)
return pme;
m_empty = false;
pme = new T();
return pme;
};
static bool reset()
{
if(m_empty)
return false;
T* pme = instance();
m_empty = true;
delete pme;
return true;
};
protected:
singleton() = default;
};
I can use the class as fellow:
class derive : public singleton<derive>
{
public:
derive() = default;
double get() const
{
return m_example;
}
private:
double m_example;
};
Q1. Is this ok please? (Please see the restriction above. I know this doesn't work in multithreaded environnement and if the instance is held).
Q2. I get error when doing this:
int main(int argc, char* argv[])
{
const double d = derive::instance()->get();
};
>main.cpp
1>main.obj : error LNK2001: unresolved external symbol "private: static bool singleton<class derive>::m_empty" (?m_empty@?$singleton@0_NA)
1>..\build\bin\Debug\windows\x86_64\tests\tests.exe : fatal error LNK1120: 1 unresolved externals
Could you please help me?
Q3. I don't need this but is there any way to have a base using CRTP class singleton in a multithreaded env?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
singleton_interface
是管理单例并调用实例的接口(我拥有此类不是用户)。最终实施:派生的类应该做:
谢谢所有!我会选择这个。
singleton_interface
is the interface managing the singletons and calls the instances (I own this class not the user). Final implementation:derived classes should do:
Thanks all! I will go with this.