c++ - 为什么在这个模板模板参数中参数推导不起作用
问题描述
我有以下模板函数,它以模板模板参数作为其参数。
template<typename T,
template <typename... ELEM> class CONTAINER = std::vector>
void merge(typename CONTAINER<T>::iterator it )
{
std::cout << *it << std::endl;
}
以下代码使用此代码。
std::vector<int> vector1{1,2,3};
merge<int>(begin(vector1));
它按预期工作,但是当我使用
merge(begin(vector1));
它不能推断出 的类型T
。
我认为它可以从std::vector<int>::iterator it;
as推断类型int
。
为什么编译器不能推断类型?
解决方案
我认为它可以从
std::vector<int>::iterator it;
int 中推断出类型。为什么编译器不能推断类型?
不。
编译器不能:查找“非推断上下文”以获取更多信息。
并且期望扣除是不合理的。
假设一个类如下
template <typename T>
struct foo
{ using type = int; };
类型type
始终为 int
; 不管是什么T
类型。
并假设一个函数如下
template <typename T>
void bar (typename foo<T>::type i)
{ }
接收一个int
值(typename foo<T>::type
总是int
)。
T
应该从以下调用中推断出哪种类型?
bar(0);
推荐阅读
- aframe - 如何在框架中加载自定义字体?
- eclipse - 无法在 Eclipse Spring 引导项目中找到或加载主类
- javascript - 如何设置边缘跳过另一个边缘?
- pytorch - 如何将微调过的 bert 模型的输出作为输入提供给另一个微调过的 bert 模型?
- javascript - 如何消除 chrome 上的生涩运动
- autodesk-forge - 是否可以在查看器中同时显示多个 Revit 模型的 2D 视图
- ruby - 如何修改作为参数传递给方法的数字
- android - 错误:无法解决:com.google.android.gms:play-services-ads:18.3.0
- docker - Docker“设置失败,缺少令牌凭据”
- .net - 在 .Net MVC 应用程序中集成 Kibana 仪表板?