I have a small program as follows:
#include <iostream>
template <typename T>
class X{
public:
bool something(){
return true;
}
};
class A: public X<A>{
};
class B: public A, public X<B>{
};
template <typename T>
bool use(T &t)
{
return t.something();
}
int main()
{
B b;
std::cout << "use returned: " << use(b);
}
This will not compile because there is an ambiguity as to which of the two possible versions of something() should be selected:
In instantiation of 'bool use(T&) [with T = B]': 30:43: required from here 22:20: error: request for member 'something' is ambiguous 6:14: note: candidates are: bool X<T>::something() [with T = B] 6:14: note: bool X<T>::something() [with T = A] In function 'bool use(T&) [with T = B]': 23:1: warning: control reaches end of non-void function [-Wreturn-type]
My question is, how can I resolve this ambiguity if the only place I can edit is the body of use()?
((A)t).something()((sometype)t).something()make sure you have the right()for order of operators.