Re: C++0x, confusion with using-declaration in multiple base classes.
On Apr 22, 11:03 pm, Johannes Schaub <schaub.johan...@googlemail.com>
wrote:
... the following is fine:
struct A {
void f();
};
struct B : A {
using A::f;
};
struct C : A {
using A::f;
};
struct D : B, C {
};
void g() { &D::f; }
In other words, member name lookup as described by 10.2 only finds A::f.
I don't understand why this is not ambiguous.
A call to B::f() is not the same as C::f(),
since B::f() is operating on the first
A subobject (from B), while C::f() is operating
on the second A subobject.
To make it more obvious, consider a more realistic example:
---------------------------------
#include <iostream>
struct A { int x; A(int i = 0) : x(i) {} int f() { return x; } };
struct B : public A { using A::f; B() : A(1) {} };
struct C : public A { using A::f; C() : A(2) {} };
struct D : public B, C {};
int main() {
A a; std::cout << a.f() << std::endl; // prints out 0
B b; std::cout << b.f() << std::endl; // prints out 1
C c; std::cout << c.f() << std::endl; // prints out 2
// D d; std::cout << d.f() << std::endl; // would print out what?
}
-----------------------------------
The last line doesn't compile under g++, but if it did,
what would you want it to print out?
The case of virtual inheritance is different, since
B::f and C::f are in this case the same code acting
on the same subobject.
But I ask: what does allowing this very special case
add to the language? And is it enough to justify
making the language more complicated? Especially
given that there is a well-known, easy-to-understand,
and quite general way to deal with the ambiguity?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]