以下代码使用GCC 4.4.6和Comeau 4.3.10进行编译.
#include <iostream>
struct A { int name; };
template<typename T> struct C : T { using T::name; };
struct B : private A { friend struct C<B>; };
int main()
{
C<B> o;
o.name = 0;
}
Run Code Online (Sandbox Code Playgroud)
它在VC++ 10中给出以下错误:
Run Code Online (Sandbox Code Playgroud)main.cpp(4): error C2877: 'A::name' is not accessible from 'A' main.cpp(10): error C2247: 'A::name' not accessible because 'B' uses 'private' to inherit from 'A'
什么是允许的良好的交叉编译器解决方案o.name = 0;?
注意:添加using A::name以B处理问题,但将A::name成员发布给每个人,而它应该只对特定模板实例可见,即C<B> …