Re: Ambiguously ambiguous?
lindahlb@hotmail.com wrote:
I'm compiling using the g++ 3.4.3, just in case this is a compiler
issue. I'm getting an odd ambiguity error that appears obviously
non-ambiguous (at least to me!).
struct A {};
struct B {};
struct Child_A : A {typedef A Parent;};
struct Child_B : B {typedef B Parent;};
template<typename Child>
struct Parent_Visitor
{
void visit(typename Child::Parent)
{
// do something depdendant on the Child type
}
};
struct Visitor :
Parent_Visitor< Child_A >,
Parent_Visitor< Child_B >
{
};
int main(int argc, char * argv[])
{
A a;
B b;
Visitor visitor;
visitor.visit(a);
visitor.visit(b);
return 0;
}
Errors from g++ 3.4.3:
request for member `visit' is ambiguous
candidates are: void Parent_Visitor<Child>::visit(typename
Child::Parent) [with Child = Child_B]
void Parent_Visitor<Child>::visit(typename
Child::Parent) [with Child = Child_A]
request for member `visit' is ambiguous
candidates are: void Parent_Visitor<Child>::visit(typename
Child::Parent) [with Child = Child_B]
void Parent_Visitor<Child>::visit(typename
Child::Parent) [with Child = Child_A]
My reasoning: Visitor has two functions it inherits, once it's parents
are instantiated, visit(A) and visit(B). Obviously, you can't
substitute the two types in any way, so why is it ambiguous, once
instantiated?
Let's simplify your test case to get rid of the templates, the private
inheritance, the typedefs, etc., and just make it a simple case of
multiple inheritance:
struct A { void visit(double); };
struct B { void visit(const char*); };
struct Visitor : public A, public B { };
int main()
{
Visitor visitor;
visitor.visit(3.5);
visitor.visit("test");
}
Compile this and you should get the same errors. Moral of the story -
multiple inheritance is weird. If both parent classes have members
with the same name, the normal overloading rules don't apply in the
derived class - you can't refer to either member unless you qualify it.
The following should compile cleanly:
struct A { void visit(double); };
struct B { void visit(const char*); };
struct Visitor : public A, public B { };
int main()
{
Visitor visitor;
visitor.A::visit(3.5);
visitor.B::visit("test");
}
Best regards,
Tom
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]