Why does the following print “Generic” instead of “const A &”? I surmised that a dynamic_cast<> would have done the trick to calling the first f but it doesn’t. Why is this?
struct A {}; struct B : A {};
template <const A &> void f() { std::cout << "const A &"; }
template <typename T> void f(T) { std::cout << "Generic"; }
int main() {
B b;
f(dynamic_cast<const A &>(b)); // "Generic"
}
The first
f()does not accept an argument, which leaves only thef(T)as a match.