Search code examples
c++c++11g++friendg++4.8

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


Consider the following C++ code:

template <class T>
class Singleton {};

class ConcreteSingleton : public Singleton<ConcreteSingleton> {
    template <class T>
    friend class Singleton;
};

int main() {}

Singleton shall be a friend of ConcreteSingleton:

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?


Solution

  • This is GCC bug #52625.

    Workaround stolen from its comments:

       template <class T>
       friend class ::Singleton;
    //              ▲▲
    

    I have verified that your code doesn't work, and this code does.