模板参数推断未按预期工作

Template argument deduction not working as expected

本文关键字:工作 参数      更新时间:2023-10-16

为什么下面的打印"通用"而不是"const A &"?我推测dynamic_cast<>会做第一个f的诀窍,但事实并非如此。这是为什么呢?

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"
}

第一个f()不接受参数,这只留下f(T)作为匹配项。