与std::lower_bound相比,ranges::lower_bound是否有不同的比较要求?
似乎在 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:'operator __surrogate_func':找不到匹配的重载函数
错误 C7602:'std::ranges::_Lower_bound_fn::operator()':不满足关联的约束
THE END
二维码