C ++<未解析的重载函数类型>具有比较功能 [英] C++ <unresolved overloaded function type> with comparison function

查看:258
本文介绍了C ++<未解析的重载函数类型>具有比较功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的二进制搜索功能如下:

 模板< typename RandomAccessIterator,typename值,typename比较器> 
inline int binary_search(RandomAccessIterator const first,RandomAccessIterator const last,Value const& value,Comparer comparer)
{
RandomAccessIterator it(std :: lower_bound(first,last,value,comparer)) ;
if(it == last || comparer(* it,value)|| comparer(value,* it))
return distance(first,last);

返回距离(first,it);
}

我使用的比较器定义如下:

 模板< class T> 
inline bool cmp(T lhs,T rhs)
{
return lhs< rhs;
}

这两个编译没有问题,但是我得到一个编译错误使用以下代码调用binary_search函数:

  binary_search(date_list.begin(),date_list.end(),date2, cmp)

其中date_list是包含日期的向量,date2是int。



确切的错误信息是:

 错误:没有匹配的函数调用?binary_search __gnu_cxx :: __ normal_iterator 
h2_lin>解决方案

你在一个上下文中传递一个模板的名称( cmp ),C ++需要一个值。除了你不能这样做,这是一个鸡或蛋的问题:什么类型是 cmp ?函数的类型取决于它的参数,这个函数可以接受任何类型的参数。那么编译器为模板参数 Comparer 推断的类型是什么?它必须查看函数的主体,以确定你期望 int ,这并不总是可能的 - 编译器不总是有权访问源代码



您需要专门选择要传递的函数模板的类型。例如:

  binary_search(date_list.begin(),date_list.end(),date2,cmp< int>); 


I am trying to implement a custom binary search to run over a vector of dates.

My binary search function is the following:

template <typename RandomAccessIterator, typename Value, typename Comparer>
inline int binary_search(RandomAccessIterator const  first, RandomAccessIterator const  last, Value const& value, Comparer comparer)
{
    RandomAccessIterator it(std::lower_bound(first, last, value, comparer));
    if (it == last || comparer(*it, value) || comparer(value, *it))
      return distance(first,last);

    return distance(first,it);
}

The comparator I am using is defined as:

template <class T>
inline bool cmp(T lhs,T rhs)
{
  return lhs<rhs;
}

These two compile without issues, however, I get a compilation error when I try to call the binary_search function using the following code:

binary_search(date_list.begin(),date_list.end(),date2,cmp)

where date_list is a vector containing dates, date2 is an int.

The exact error message is:

error: no matching function for call to ?binary_search(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int&, <unresolved overloaded function type>)?

Any ideas on how to solve this?

解决方案

You're passing the name of a template (cmp) in a context where C++ wants a value. Apart from the fact that you can't do this, it's a chicken-or-egg problem: what type is cmp? A function's type depends on its arguments, and this function can take arguments of any type. So what type does the compiler infer for the template argument Comparer? It would have to look at the body of the function to figure out that you expect int, and that's not always possible—the compiler doesn't always have access to the source code of templates.

You need to specifically select the type for the function template that you're passing. For example:

binary_search(date_list.begin(), date_list.end(), date2, cmp<int>);

这篇关于C ++&lt;未解析的重载函数类型&gt;具有比较功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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