Why does this std::sort predicate fail when the class is inside main()?

Viewed 1249

This is a much simplified repro which illustrates how class Predicate delcared outside main() works but when the exact code appears inline as class InlinePredicate the compiler can't match std::sort. The strange thing is that you can pass anything as the third argument to std::sort (say, integer 7) and you'll just get a compile error when it does not support the operator () that sort expects. But when I pass pred2 below it doesn't match at all:

#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: In function ‘int main()’:

repro.cc:30: error: no matching function for call to ‘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&)’

2 Answers
Related