3

The code below does not compile with both MSVC2017 and GCC11:

#include <deque>
#include <chrono>
#include <algorithm>

using Clock = std::chrono::system_clock;
using TimePoint = std::chrono::time_point<Clock>;

struct PricePoint
{
    TimePoint dt;
    double buy;
    double sell;
};

inline bool operator < (const TimePoint & dt, const PricePoint & a)
{
    return a.dt < dt;
}

int main()
{
    std::deque<PricePoint> priceSequence;
    const auto begin = std::lower_bound(priceSequence.begin(), priceSequence.end(), Clock::now());
    return 0;
}

But if I replace std::lower_bound with std::upper_bound it starts to compile. What is the difference?

Dmitriano
  • 1,878
  • 13
  • 29

1 Answers1

9

error: no match for 'operator<'

This sort of error suggests that some template code is trying to use an operator you haven't defined.

lower_bound and upper_bound do opposite comparisons.< (const TimePoint & dt, const PricePoint & a) is fine for upper_bound, but lower_bound needs you to define this:

inline bool operator < (const PricePoint & a, const TimePoint & dt)
{
    return dt < a.dt;
}
Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75