【发布时间】:2011-10-16 08:13:21
【问题描述】:
这是一个非常简化的重现,它说明了 class Predicate 在 main() 之外如何工作,但是当确切的代码内联显示为 class InlinePredicate 时,编译器无法匹配 std::sort。奇怪的是,你可以将 anything 作为第三个参数传递给std::sort(比如整数7),当它不支持operator () 时,你只会得到一个编译错误。 987654327@ 预计。但是当我通过下面的pred2 时,它根本不匹配:
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Predicate {
public:
bool operator () (const pair<string,int>& a, const pair<string,int>& b)
{
return a.second < b.second;
}
};
int
main()
{
vector<pair<string, int> > a;
Predicate pred;
sort(a.begin(), a.end(), pred);
class InlinePredicate {
public:
bool operator () (const pair<string,int>& a, const pair<string,int>& b)
{
return a.second < b.second;
}
} pred2;
sort(a.begin(), a.end(), pred2);
return 0;
}
repro.cc:在函数“int main()”中:
repro.cc:30: 错误: 没有匹配函数调用 'sort(__gnu_cxx::__normal_iterator, std::allocator >, int>*, std::vector, std::allocator >, int>, std ::allocator, std::allocator >, int> > > >, __gnu_cxx::__normal_iterator, std::allocator >, int>*, std::vector, std::allocator >, int>, std::allocator, std::allocator >, int> > > >, main()::InlinePredicate&)'
【问题讨论】:
-
附带说明:您的运营商可能应该是 const
bool operator()(const pair<string,int>& a, const pair<string,int>& b) **const**