I would like some clarification as to the behavior of the following:
#include <iostream>
class A {};
class B : public A {};
class Q : public A {};
class C : public B {};
void doOverload(const A& v)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void doOverload(const B& v)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void doOverload(const C& v)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
int main()
{
A a;
B b;
C c;
Q q;
doOverload(a);
doOverload(b);
doOverload(c);
doOverload(q);
}
If I compile and run this using GCC on my PC, the output is:
void doOverload(const A&)
void doOverload(const B&)
void doOverload(const C&)
void doOverload(const A&)
Which means that, for example, even though type B "is a" A, the compiler chooses the override for the most specific type. Likewise, Q is also an A but no specific overloaded method exists for Q so the overload for A is chosen.
My question is, is this guaranteed by the standard? If yes, where in the standard?