模板参数推断未按预期工作
Template argument deduction not working as expected
为什么下面的打印"通用"而不是"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)
作为匹配项。
相关文章:
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 如何定义在用作函数参数时工作的类模板的转换
- C++20字符串文本模板参数工作示例
- C++中的可变参数模板及其工作原理
- std::bind,无法让具有单个参数的方法工作
- 隐式转换是否应该在模板参数的上下文中工作?
- 我不明白参数和参数如何协同工作
- 通过参数启用时,std::enabled_if 如何工作
- 当传递NULL作为参数时,函数重载是如何工作的
- GoogleTest 中的参数化测试无法按预期工作
- 此函数如何在不传递任何参数的情况下工作?
- 我的代码在作为参数传入 .begin() 时不起作用,但在我将 .begin() 转换为迭代器后工作
- C++11: 如何编写一个像 Get 一样工作的模板函数<tuple>...但是收到参数包?
- 参数传递和返回值如何在程序集级别的 x86 上的 C/C++ 中工作?
- 我可以更改将引用参数传递到指针中并使其正常工作的函数的输入(C、C++)吗?
- 工作函数重载如何使用可变参数解析?
- 宏参数的扩展如何在 c++ 中工作
- 错误太多参数无法正常工作
- 具有可变参数的std ::函数列表如何工作
- C++参数化构造函数使代码在传递大输入时停止工作