Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 948
sergej Avatar asked Oct 20 '22 01:10

sergej


1 Answers

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.

like image 61
Lightness Races in Orbit Avatar answered Oct 31 '22 11:10

Lightness Races in Orbit