I'm trying to understand how C++/Clang deals with static members in templates. To that end I defined a Singleton
as follows:
template <class T> class Singleton {
public:
static T* instance() {
if (!m_instance)
m_instance = new T;
return m_instance;
}
private:
static T* m_instance;
};
template <class T> T* Singleton<T>::m_instance = nullptr;
This appears to work just fine provided my application is compiled as a single executable. When I start using plugins, i.e. dylib
s opened with dlopen
, I get multiple instances of the Singleton
.
Normally I compile my application with -fvisiblity=hidden
. If remove that option, meaning I use default
visibility, then the Singleton
s behave properly. This led me to think I just need to export the symbol using __attribute__((visibility=default))
but this doesn't work.
What is going here, and what would the solution be?