lundi 28 septembre 2015

g++ error: specialization after instantiation (template class as friend)

Consider the following C++ code. I have two classes Singleton and ConcreteSingleton. ConcreteSingleton is derived from Singleton. So far so good...

Now Singleton shall be a friend of ConcreteSingleton.

// Singleton (C++11)
template <class T>
class Singleton {
protected:
    Singleton() {}
public:
    static T& getInstance() {
        static T instance;
        return instance;
    }
};

// Concrete Singleton
class ConcreteSingleton : public Singleton<ConcreteSingleton> {
private:
    template <class T> friend class Singleton; // error: specialization after instantiation
    ConcreteSingleton() {}
public:
    void foo() {}
};

It works with Microsoft's visual C++ compiler. But, I can't compile it with g++ 4.8.4. The error is:

   error: specialization of ‘Singleton<ConcreteSingleton>’ after instantiation
       template <class T> friend class Singleton;

Is there any way to fix it?

Aucun commentaire:

Enregistrer un commentaire