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

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

考虑以下 C++ 代码:

template <class T>
class Singleton {};

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

int main() {}

Singleton 应是 ConcreteSingleton 朋友:

它适用于 Microsoft 的可视化 C++ 编译器。但是,我不能用 g++ 4.8.4 编译它。错误是:

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

有什么办法可以解决吗?

这是GCC bug #52625

从其评论中窃取的解决方法:

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

我已经验证 your code doesn't work, and this code does