我似乎有点简单,因为我在下面标记为错误的行上不太清楚此错误的原因。
std::sort and boost::sort 选取默认谓词,但由于某种原因,ranges-v3 不会。这是范围-v3 0.36。在 clang 6/7 和 gcc 7/8 上也有类似的错误。
#include <range/v3/all.hpp>
#include <algorithm>
#include <boost/hana.hpp>
#include <utility>
#include <vector>
#include <boost/range/algorithm.hpp>
namespace hana = boost::hana;
template< typename T = int>
struct point_t {
BOOST_HANA_DEFINE_STRUCT(point_t<T>, (T, x), (T, y));
constexpr bool operator<(const point_t<T> &b) const noexcept {
return hana::less(hana::to_tuple(*this), hana::to_tuple(b));
};
};
int main() {
std::vector<point_t<point_t<>>> all;
boost::sort(all); // OK
std::sort(std::begin(all), std::end(all)); //OK
ranges::sort(all, std::less<point_t<point_t<>>>()); // OK
ranges::sort(all, hana::less); // OK
ranges::sort(all); // error no matching function for call to object of type 'const with_braced_init_args<ranges::v3::sort_fn>'
return 0;
}
Casey 通过 range-v3 问题列表快速回答。
以下是他的评论,根据要求作为文本,而不是我放在这里的原始图像:
没有比较器参数
ranges::sort
要求类型为 排序以对StrictTotallyOrdered
概念进行建模。这意味着 该类型必须定义所有==
、!=
、<
、>
、<=
和>=
一致的语义。
我在那里回答说:
谢谢你这么快回来。我现在明白了。我必须说 有点令人失望的是它与
std::sort
和不兼容boost::sort
要求。这就是我们付出的代价range-v3
我猜很可爱。再次感谢,--马特。
不幸的是,要求高于std::sort
和boost::sort
,因此代码不仅有效。我理解动机。
对于好奇的人来说,std::rel_ops
和boost/operators
似乎干扰了我对可内省结构的聚合初始化支持的目标,所以我最终求助于宏(类似于下面(,可悲的是。
我会再玩一些,寻找更好的静态多态解决方案。
亲切问候
--马 特。
#define JEST_STRUCT(T)
constexpr bool operator==(const T &b) const noexcept {
return hana::equal(hana::to_tuple(*this), hana::to_tuple(b));
};
constexpr bool operator!=(const T &b) const noexcept {
return hana::not_equal(hana::to_tuple(*this), hana::to_tuple(b));
};
constexpr bool operator<(const T &b) const noexcept {
return hana::less(hana::to_tuple(*this), hana::to_tuple(b));
};
constexpr bool operator<=(const T &b) const noexcept {
return hana::less_equal(hana::to_tuple(*this), hana::to_tuple(b));
};
constexpr bool operator>(const T &b) const noexcept {
return hana::greater(hana::to_tuple(*this), hana::to_tuple(b));
};
constexpr bool operator>=(const T &b) const noexcept {
return hana::greater_equal(hana::to_tuple(*this), hana::to_tuple(b));
}