简体   繁体   English

为什么参数推导在此模板模板参数中不起作用

[英]Why parameter deduction doesn't work in this template template parameter

I have following template function which has template template parameter as its argument. 我有以下模板函数,其中模板模板参数作为其参数。

template<typename T, 
         template <typename... ELEM> class CONTAINER = std::vector>
void merge(typename CONTAINER<T>::iterator it )
{
   std::cout << *it << std::endl;
}

And the following code uses this code. 以下代码使用此代码。

std::vector<int> vector1{1,2,3};
merge<int>(begin(vector1));

It works as expected, but when I use 它按预期工作,但是当我使用时

merge(begin(vector1));

It cannot deduce type of T . 它不能推断出T类型。

I thought that it could deduce type from std::vector<int>::iterator it; 我认为它可以从std::vector<int>::iterator it;推断出类型std::vector<int>::iterator it; as int . 作为int

Why the compiler can't deduce the type? 为什么编译器无法推断出类型?

I thought that it could deduce type from std::vector<int>::iterator it; 我认为它可以从std::vector<int>::iterator it;推断出类型std::vector<int>::iterator it; as int. 作为int。

Why the compiler can't deduce the type? 为什么编译器无法推断出类型?

No. 没有。

The compiler can't: look for "non-deduced context" for more information. 编译器不能:查找“非推导上下文”以获取更多信息。

And isn't reasonable expecting a deduction. 并且期望扣除是不合理的。

Suppose a class as follows 假设一个类如下

template <typename T>
struct foo
 { using type = int; };

where the type type is always int ; 类型type 总是 int ; whatever is the T type. 无论什么是T型。

And suppose a function as follows 并假设功能如下

template <typename T>
void bar (typename foo<T>::type i)
 { }

that receive a int value ( typename foo<T>::type is always int ). 接收int值( typename foo<T>::type始终为int )。

Which T type should be deduced from the following call ? 应从以下调用中推断出哪种T类型?

bar(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM