I have faced some code snippets that a class can inherited from another class via its template parameter. However, I do not understanding why I need to have the three using line in struct D. I try to call the say_hi() function of d but it states error: request for member ‘say_hi’ is ambiguous and the error gone if I have the three using lines. Can anyone tell me why?
struct C1 {
void say_hi(int) { puts("int hi"); }
};
struct C2 {
void say_hi(char) { puts("char hi"); }
};
struct C3 {
void say_hi(double) { puts("double hi"); }
};
template<typename T, typename U, typename V>
struct D : T, U, V {
using T::say_hi;
using U::say_hi;
using V::say_hi;
};
D<C1, C2, C3> d;
This is a variation on how the hiding rule works in c++ : https://isocpp.org/wiki/faq/strange-inheritance#hiding-rule . But you actually dont declare any method in D which could overload the base class methods. C++ compiler does not search for all possible overloads of your function call in whole inheritance tree but stop when it finds a correct overload (adhering to standard). To modify this base working of compiler, you can add
usingdeclarations to specify which overloads should be visible or to resolve ambiguities.