【发布时间】:2021-06-29 08:30:00
【问题描述】:
似乎在 C++20 中使用与 std::lower_bound() 正常工作的相同比较函子不适用于 std::ranges::lower_bound()。以下代码不能与 Visual Studio 16.10.2(和 /std::c++latest)或 gcc 11.1 一起编译。我可以通过使用投影而不是比较函子来解决这个问题,但这会增加迁移的工作量。
std::ranges::lower_bound() 中参数“comp”的要求是否与 std::lower_bound() 不同,如果是,如何?
#include <algorithm>
#include <vector>
struct A {
double m_value;
};
struct MyComparison {
bool operator()(const A& a, const double& b) {
return a.m_value < b;
}
};
void f() {
std::vector<A> v = {A{1.0}, A{2.0}, A{3.0}};
// comparison functor without ranges compiles as expected:
auto x = std::lower_bound(v.cbegin(), v.cend(), 2.0, MyComparison());
// projection with ranges compiles as expected:
auto y = std::ranges::lower_bound(v, 2.0, {}, &A::m_value);
// comparison functor with ranges fails to compile:
auto z = std::ranges::lower_bound(v, 2.0, MyComparison());
}
Visual Studio 中的错误消息:
错误 C2672:“运算符 __surrogate_func”:未找到匹配的重载函数
错误 C7602: 'std::ranges::_Lower_bound_fn::operator ()': 不满足相关约束
【问题讨论】: