I'm working with some classes which are expected to perform as a singleton. My code works like this:
template <typename T>
struct Singleton {
public:
using SingletonType = T;
protected:
static T& getInstance() {
static T instance;
return instance;
}
};
struct NormalClass: protected Singleton<NormalClass> {
//The following three seem to be the same
//friend class Singleton;
//friend class Singleton<NormalClass>;
friend class Singleton<SingletonType >;
static const SingletonType& instance() {return Singleton<SingletonType>::getInstance();}
protected:
NormalClass();
};
But when I work with a template class, it forces me to explicitly declare the typename for Singleton and I cannot access Singleton::SingletonType
template <int N>
struct TemplateClass: protected Singleton<TemplateClass<N>>{
friend class Singleton<TemplateClass>; // It works fine
//friend class Singleton<SingletonType >; //error: 'SingletonType' was not declared in this scope;
protected:
int n;
TemplateClass():n(N){}
};
Could someone explain why to me? Thanks for your answer.