为什么参数推导在这个模板模板参数中不起作用
Why parameter deduction doesn't work in this template template parameter
我有以下模板函数,它的参数是模板模板参数。
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;
推断类型为 int
。
为什么编译器无法推断类型?
I thought that it could deduce type from std::vector<int>::iterator it;
as int.
Why the compiler can't deduce the type?
没有
编译器无法:查找“non-deduced 上下文”以获取更多信息。
期望扣除是不合理的。
假设class如下
template <typename T>
struct foo
{ using type = int; };
其中类型 type
是 always int
; T
类型是什么。
并假设一个函数如下
template <typename T>
void bar (typename foo<T>::type i)
{ }
收到 int
值(typename foo<T>::type
始终是 int
)。
应从以下调用中推导出哪种 T
类型?
bar(0);
我有以下模板函数,它的参数是模板模板参数。
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;
推断类型为 int
。
为什么编译器无法推断类型?
I thought that it could deduce type from
std::vector<int>::iterator it;
as int.Why the compiler can't deduce the type?
没有
编译器无法:查找“non-deduced 上下文”以获取更多信息。
期望扣除是不合理的。
假设class如下
template <typename T>
struct foo
{ using type = int; };
其中类型 type
是 always int
; T
类型是什么。
并假设一个函数如下
template <typename T>
void bar (typename foo<T>::type i)
{ }
收到 int
值(typename foo<T>::type
始终是 int
)。
应从以下调用中推导出哪种 T
类型?
bar(0);