class A { public: int a; };
class B : public virtual A { public: using A::a; };
class C : public virtual A { public: using A::a; };
class D : public C, public B { };
class W { public: int w; };
class X : public virtual W { public: using W::w; };
class Y : public virtual W { };
class Z : public Y, public X { };
int main(){
D d;
d.a = 0; // Error
Z z;
z.w = 0; // Correct
return 0;
}
第一组类声明(A
、B
、C
和D
)和第二组( W
、X
、Y
和 Z
)的构建方式类似,只是 class C
有一个 using 声明(using A::a
)而 class Y
没有。
当尝试访问 d.a = 0
中的成员 a
时,我在 Clang 中遇到错误(error: member 'a' found in multiple base classes of different types
) 和 GCC 中(error: request for member 'a' is ambiguous
)。我检查了两个编译器的最新版本和旧版本,它们都同意。但是,访问 z.w = 0
中的 w
编译成功。
访问a
时出现这种歧义的原因是什么?
据我所知,类 B
和 C
中的访问声明都引用相同的基类成员。顺便说一句,如果我删除它们,测试将成功编译,因为 a
已经可以公开访问( public
访问说明符)。
提前致谢。
注意:以上代码是对SolidSands 稍作修改的测试' super 测试套件。
最佳答案
这里存在实现差异; ICC 接受您的代码,而 gcc、clang 和 MSVC 拒绝它。 ICC 是正确的,其他编译器是错误的。
运行 [class.member.lookup] D::a
的算法,我们发现:
- 没有
a
的申报在D
,所以 S(a, D) 最初是空的,我们合并到a
的查找集中在其基类中,计算如下:- S(a, B) = { { A::a }, { B } }
- S(a, C) = { { A::a }, { C } }
- 生成的查找集是 S(a, D) = { { A::a }, { B, C } }
请注意,在 S(a, B) 的声明集中,成员是 A::a
即使它在 B
中找到,对于 S(a, C) 也类似:
In the declaration set, using-declarations are replaced by the set of designated members [...]
判断成员是否访问d.a
是不明确的,我们现在检查 [expr.ref]/5:
5 - [The] program is ill-formed if the class of which
E2
is directly a member is an ambiguous base of the naming class ofE2
[...]
在这里E2
已确定为A::a
, 的直接成员 A
.命名类是D
. A
不是 D
的模糊基数, 自 A
是 D
的所有中间基类子对象的虚拟基类.所以d.a
在名称查找和成员访问方面都是明确的,并且您的程序是正确的。
作为一个类似的实例,我们可以考虑根据 [class.member.lookup]/9 的注释用静态成员替换虚拟继承:
9 - [ Note: A static member, a nested type or an enumerator defined in a base class
T
can unambiguously be found even if an object has more than one base class subobject of typeT
. Two base class subobjects share the non-static member subobjects of their common virtual base classes. — end note ]
struct A { static int a; };
struct B : A { using A::a; };
struct C : A { using A::a; };
struct D : B, C { };
int main() {
D d;
d.a = 0; // OK
}
这里我们又一次有 S(a, D) = { { A::a }, { B, C } }。事实上,即使 A::a
名称查找也以相同的方式进行。是非虚拟基础的非静态成员; 名称查找 是明确的,但成员访问 [expr.ref] 在这种情况下是不明确的。
为了进一步阐明名称查找和成员访问之间的区别,请考虑即使对于非虚拟多重继承基类的非静态数据成员,也可以明确地使用采用派生类的成员的名称作为命名类:
struct A { int a; };
struct B : A { using A::a; };
struct C : A { using A::a; };
struct D : B, C { };
int B::*p = &D::i; // OK, unambiguous
不幸的是,尽管这是(模使用声明)an example in the Standard,但我尝试过的每个编译器都拒绝了它!
关于c++ - C++ 成员名称查找和访问声明中的歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44626415/