Template type deduction in C++ for Class vs Function?(C++ 中类与函数的模板类型推导?)
问题描述
为什么自动类型推导只能用于函数而不能用于类?
Why is that automatic type deduction is possible only for functions and not for Classes?
推荐答案
在特定情况下,你总是可以像 std::make_pair:
In specific cases you could always do like std::make_pair:
template<class T>
make_foo(T val) {
return foo<T>(val);
}
我刚刚在C++ 编程语言,第三版",第 335 页中找到了以下内容.Bjarne 说:
I just found the following in "The C++ Programming Language, Third Edition", page 335. Bjarne says:
请注意,类模板参数是从未推断.原因是几个提供的灵活性类的构造函数将使这种扣除在很多情况下是不可能的案例和模糊的更多.
Note that class template arguments are never deduced. The reason is that the flexibility provided by several constructors for a class would make such deduction impossible in many cases and obscure in many more.
这当然是非常主观的.在comp.std.c++ 中对此进行了一些讨论,共识似乎是没有理由不支持它.这是否是一个好主意是另一个问题...
This is of course very subjective. There's been some discussion about this in comp.std.c++ and the consensus seems to be that there's no reason why it couldn't be supported. Whether it would be a good idea or not is another question...
这篇关于C++ 中类与函数的模板类型推导?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 中类与函数的模板类型推导?
基础教程推荐
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
