在函数中使用模板模板参数没有匹配的函数错误 [英] no matching function error using template template parameters in a function

查看:208
本文介绍了在函数中使用模板模板参数没有匹配的函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模板模板参数定义一个函数(我只想知道它的工作原理).我有以下内容:

I'm trying to define a function using template template parameters (I just want to know how it works). I have the following:

template <typename T, template <typename> class Cont>
typename Cont<T>::iterator binary_search (typename Cont<T>::iterator first, typename Cont<T>::iterator last)
{
    typename Cont<T>::iterator it;
    // ...
    return it;
}

然后在main ()函数中:

std::vector<int> data;

// ....

std::vector<int>::iterator it = binary_search (data.begin (),data.end ());

尝试编译代码时出现此错误:

I get this error when trying to compile the code:

binary_search.cpp: In function ‘int main(int, char**)’:
binary_search.cpp:43:83: error: no matching function for call to ‘binary_search(std::vector<int>::iterator, std::vector<int>::iterator)’

我找不到任何适当的响应来帮助我解决此错误.任何帮助将不胜感激.

I cannot find any appropriate response that helps me to sort out this error. Any help would be appreciated.

预先感谢

推荐答案

即使上下文是可推论的,您所拥有的也是一个非推论上下文,以及模板模板参数不匹配. std::vector使用第二个模板参数,即分配器,默认为std::allocator.

What you have is a non-deduced context, aswell as a template template parameter mismatch even if the context was deducible. std::vector takes a second template parameter, the allocator, that is defaulted to std::allocator.

对于非推导上下文,T永远不能被推导,并且总是必须被指定,typename指示了这一点. 有关血腥细节,请参见此问题.

For the non-deduced context, T can never be deduced and will always have to be specified, the typename indicates this. See this question for the gory details.

这篇关于在函数中使用模板模板参数没有匹配的函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆