2

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.

  • See duplicates: `SingletonType` is a non-dependent name which won't be looked up in dependent base classes. You would need to qualify the name with the base class. – user17732522 Feb 07 '23 at 07:15

0 Answers0