【发布时间】:2026-02-02 18:15:01
【问题描述】:
以下代码使用 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;
}
VC++10 报如下错误:
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 成员发布给所有人,而它应该只对特定模板实例化可见,即@ 987654328@.
【问题讨论】:
-
不应该是
using T::name;吗?这个语言功能怎么称呼? -
如果将
using A::name;添加到B中会发生什么? -
@Nick:添加
using A::name没有任何缺点。按照标准应该是没有必要的,但绝对没有不良影响。名称查找出现在访问控制之前。但它也不会造成伤害,也不会妨碍您,所以请随意将其留在里面。 -
其实你可以说
B确实有一个成员,即私有继承的A实例。
标签: c++ templates visual-c++ private-inheritance