下面的代码不同时使用MSVC2017和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;
}但是,如果我将std::lower_bound替换为std::upper_bound,它将开始编译。有什么关系?
发布于 2020-09-24 20:45:46
错误:与“operator<”不匹配
这种错误表明某些模板代码试图使用未定义的操作符。
lower_bound和upper_bound做了相反的比较。< (const TimePoint & dt, const PricePoint & a)适用于upper_bound,但是lower_bound需要定义如下:
inline bool operator < (const PricePoint & a, const TimePoint & dt)
{
return dt < a.dt;
}https://stackoverflow.com/questions/64054070
复制相似问题