You can use boost::hana::overload
to overload lambdas:
auto constexpr comp = boost::hana::overload(
[]( const S& s, int i ) { return s.number < i; },
[]( int i, const S& s ) { return i < s.number; }
);
const auto p2 = std::equal_range(vec.begin(),vec.end(), 2, comp);
Obviously you can put the whole boost::hana::overload(…)
thing in the call to std::equal_range
, but it just makes the code less readable, imho.
However, I would stress that whether you write it yourself or not, you'll likely end up with using a struct
/class
; e.g. std::function
is a templated class
, boost::hana::overload
is an object of a given class
, and so on.
Since you haven't specified a standard, probably using Jeff Garrett solution is the way to go (though I'd change vec.begin(), vec.end(),
to vec,
, if you are to process the whole vec
). If you have to be C++17 compliant, than you can use Range-v3:
#include <range/v3/algorithm/equal_range.hpp>
// ...
const auto p2 = ranges::equal_range(vec, 2, std::less{},
[](const S& s) { return s.number; });
which is ok for C++14 too, as long as you don't rely on CTAD:
const auto p2 = ranges::equal_range(vec, 2, std::less<>{},
[](const S& s) { return s.number; });