C++ 模板嵌套私有类作为朋友
我有以下代码没有在GCC4.9和GCC5.1上编译。我似乎不明白为什么。对不起,如果这是一个NoOB问题,我对C++模板有点新。C++ 模板嵌套私有类作为朋友,c++,templates,gcc,C++,Templates,Gcc,我有以下代码没有在GCC4.9和GCC5.1上编译。我似乎不明白为什么。对不起,如果这是一个NoOB问题,我对C++模板有点新。 template<class T>class A { template<class X>friend class B; struct C {}; }; template<class X>class B { template<class T>friend struct A<T>::C
template<class T>class A
{
template<class X>friend class B;
struct C {};
};
template<class X>class B
{
template<class T>friend struct A<T>::C;
};
int main()
{
A<int> a;
B<float> b;
}
非常感谢您的帮助,或者如果您已经提出了这样的问题,请共享该链接。在这种情况下,您从clang那里得到的警告会更有帮助:
warning: dependent nested name specifier 'A<X>::' for friend class declaration is not supported
另一种选择是使用
模板类A代码>在B
的内部,这也会使A::C
成为朋友。对不起,我的错误,错误的评论,纠正了C
是模板化的,因为它在A
@RyanHaining的内部。我想我必须学习很多关于模板的知识:pYou关于clang是正确的,只是用clang++3.5检查一下,就得到了错误。但是我不想要依赖别人的朋友。意味着A::C和B也可以成为朋友!!
class A
{
friend class B;
class C{};
};
class B
{
friend class A::C;
};
int main()
{
A a;
B b;
}
warning: dependent nested name specifier 'A<X>::' for friend class declaration is not supported
template <typename T> class B;
template<class T>
class A {
friend class B<T>; // B of the same templated type is my friend
struct C {};
};
template<class T>
class B {
friend struct A<T>::C; // A::C of the same templated type is my friend
};