c++ - 为什么我不能将两个不同的比较器传递给一个模板函数?

标签 c++ templates comparator c2664

我在这里绞尽脑汁好几个小时,但我仍然不明白为什么我在尝试运行这段代码时会遇到错误。 一段时间后,我设法将其缩小为以下表达式:

pastryPrice()

导致问题的原因 - 如您所见,我正在尝试为排序的一个模板函数构建大量比较器

    struct dialingAreaComp{
    inline bool operator()(const Deliver *d1, const Deliver *d2)const {
        return d1->getDialingArea() < d2->getDialingArea();
    }
};
struct pastryPrice {
    inline bool operator()(const Pastry *p1, const Pastry *p2)const {
        return p1->getPrice() < p2->getPrice();
    }
};
template<class T>
void sortCollection(T& collection)
{
    if ( typeid (collection) == typeid(vector <Deliver*>))
    {
        sort(collection.begin(), collection.end(), dialingAreaComp());
        printCollection(collection);
    }
    else if (typeid (collection) == typeid(vector <Pastry*>))
    {
        sort(collection.begin(), collection.end(), pastryPrice());
        printCollection(collection);
    }
    else { cout << "WRONG!"; }
}

我得到五个错误,都是一样的:

Severity Code Description Project File Line Suppression State Error C2664 'bool Bakery::pastryPrice::operator ()(const Pastry *,const Pastry *) const': cannot convert argument 1 from 'Deliver *' to 'const Pastry *' Bakery c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 809

还有一个:

Severity Code Description Project File Line Suppression State Error C2056 illegal expression Bakery c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 809

当我去掉上面写的表达式时,代码运行良好 - 为什么我不能将两个不同的比较器传递给一个模板函数?

现在:

C2264 is a Compiler Error that occurs when one tries to pass a function a parameter of an incompatible type.

但是 Deliver 函数有效,当我取消 Deliver 比较器时,Pastry 也编译了......那么不兼容的类型是什么?

最佳答案

你的问题是无论采用哪个分支都会编译两个分支。

我会以不同的方式处理这个问题。

template<class A, class B>
struct overload_t:A,B{
  using A::operator();
  using B::operator();
  overload_t(A a, B b):A(std::move(a)), B(std::move(b)){}
};
template<class A, class B>
overload_t<A,B> overload( A a, B b ){
  return {std::move(a),std::move(b)};
}

这让我们可以重载两个函数对象或 lambda。 (可以添加完美转发,可变参数也可以...,但我保持简单)。

现在我们只需:

auto comp=overload(dialingAreaComp{}, pastryPrice{});
using std::begin; using std::end;
std::sort( begin(collection), end(collection), comp );

编译器会为我们选择正确的比较函数。我在那里时还支持平面阵列。

并停止使用 using namespace std;


以上代码所做的是将您的两个函数对象类型融合为一个。 using A::operator()using B::operator()() 移动到同一个类中,并告诉 C++ 选择在使用通常的方法调用重载规则调用时在它们之间。其余代码是胶水,用于推断被重载的类型并移动构造它们。

sort 使用编译时根据容器类型确定类型的对象调用 ()。重载解析(在调用点的 sort 内)然后选择正确的主体在编译时进行比较。

因此,可以通过支持 2 个以上的重载、函数指针和转发引用来扩展技术。在 C++17 中,可以做一些工作让重载类型推导其父类型,从而消除对工厂函数的需要。

关于c++ - 为什么我不能将两个不同的比较器传递给一个模板函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40175371/

相关文章:

java - 为什么通用类型参数说 "extends"Comparable 而不是 "implements"?

c++ - 关于原生 C++ 开发的现代书籍

c++ - C++ 中的单一错误

c++ - 在 C/C++ 中使用 SDL2_gfx 绘制实心圆

C++模板函数选择

java - 使用 spring 的比较器实例化优先级队列

c++ - 具有相同变量名的成员的 setter 行为是在 C++ 中确定的吗?

c++ - STL 容器 list、deque、vector 等的基类是什么?

c++ - vect.hpp:13:33:错误: ‘operator<<’声明为无效

java - 为什么不使用自定义比较器从TreeSet中删除,则会删除较大的项集?