template boy
template boy

Reputation: 10480

Template argument deduction not working as expected

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"

}

Upvotes: 1

Views: 52

Answers (1)

hmjd
hmjd

Reputation: 121961

The first f() does not accept an argument, which leaves only the f(T) as a match.

Upvotes: 5

Related Questions