C++11 - binding sort function

Viewed 811

I'd like to save myself some typing and therefore define something like this:

using namespace std;

vector<MyClass> vec;

auto vecsort = bind(sort, vec.begin(), vec.end(), [] (MyClass const &a, MyClass const &b) {
        // custom comparison function
    });

vecsort(); // I want to use vecsort() a lot afterwards

For some reason this doesn't compile - why?

Using boost is not an option.

Minimal working example:

#include <vector>
#include <utility>
#include <algorithm>
#include <functional>

using namespace std;

int main() {

    vector<pair<int, int>> vec;
    for (int i = 0; i < 10; i++)
        vec.push_back(make_pair(10 - i, 0));

    auto vecsort = bind(sort, vec.begin(), vec.end(), [] (pair<int, int> const &a, pair<int, int> const &b) {
            return a.first < b.first;
        });

    vecsort();

}

Error:

error: no matching function for call to 'bind(<unresolved overloaded function type>, std::vector<std::pair<int, int> >::iterator, std::vector<std::pair<int, int> >::iterator, main()::__lambda0)'

3 Answers
Related