How to fix a purported lack of an "explicit instantiation declaration" when compiling a CRTP Singleton with Clang?
The simplest fix is to define instance_
in SingletonBase.hh:
template < class T > class SingletonBase {
public:
static T * get_instance() {
if ( ! instance_ ) {
instance_ = T::create_singleton_instance();
}
return instance_;
}
private:
static T * instance_;
};
template <typename T>
T* SingletonBase<T>::instance_ = nullptr;
However, I don't see the point of SingletonBase
if you are going to rely on T::create_singleton_instance()
to create the instance. You might as well implement get_instance()
in the derived class.
Using a CRTP to implement the singleton pattern makes sense only if the base class can construct an instance of the derived class using the default constructor.
template < class T > class SingletonBase {
public:
static T& get_instance() {
static T instance_;
return instance_;
}
private:
};
Further reading: How to implement multithread safe singleton in C++11 without using <mutex>