Consider the template function sort(...) below. This is a wrapper function around std::sort. The purpose is to provide a better syntax when sorting vectors of user-defined classes. The first argument is the vector to be sorted. The second argument is a function specifying how the vector is to be sorted (on which public member variable or function).
std::sort require a compare-function in order to rank the different items in the vector. This is declared as a lambda inside my sort-function. However, this code only compiles if the lambda is declared 'auto', not if it is declared as bool. I find this strange. Could someone please explain?
(The code should compile as it stands now. To observe the problem, uncomment the line beginning with 'bool').
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Copy function is needed in sort(...), below
template<typename T>
auto copy(const std::vector<T>& v)
{
auto r = std::vector<T>();
r.reserve(v.size());
for(const auto & e : v) {
r.push_back(e);
}
return std::move(r);
}
template<typename T, typename F>
auto sort(const std::vector<T>& v, const F& f)
{
auto r = copy(v);
// ///////////// Consider the following line: /////////////////
// bool compare = [=](const T& lhs, const T& rhs) // does not compile, why?
// auto compare = [=](const T& lhs, const T& rhs) -> bool // compiles
auto compare = [=](const T& lhs, const T& rhs) // compiles
{
return f(lhs) < f(rhs);
};
std::sort(r.begin(), r.end(), compare);
return std::move(r);
}
struct Foo {
int id;
string message;
};
int main()
{
auto unordered = vector<Foo>();
unordered.push_back(Foo{2, "the last element"});
unordered.push_back(Foo{1, "the first element"});
// I want to sort the vector by 'id'.
auto ordered = sort(unordered, [](const auto& e) { return e.id; });
cout << "Ordered vector (by id):" << endl;
for(const auto& e : ordered) {
cout << e.id << ", " << e.message << endl;
}
return 0;
}