boost - 范围-v3 / 排序混淆
问题描述
看起来我有点简单,因为我不太能够在下面标记为错误的行上清楚地看到此错误的原因。
std::sort 和 boost::sort 选择默认谓词,但 range-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)); \
}
推荐阅读
- ruby - 使用 send 从 ruby 中的对象实例调用时指定父类
- django - 尝试迁移表时出现 STATICFILES_DIRS 错误 - django heroku
- java - JavaFX:将 ObservableList 绑定到哪个视图?
- javascript - 'then' 和 'catch' 一个接一个的叫
- css - 使用 vuetify 将 btn 定位到对话框的绝对右上角
- database - 我的项目无法连接到 Oracle 10G 数据库
- javascript - 我的页面上只显示了一个 D3 可视化?
- orm - Chainlink 节点:无法锁定 ORM:拨打 tcp 127.0.0.1:5432:连接:连接被拒绝 logger/default.go:155 stacktrace=gith
- flutter - Dart:从值中找到键并添加到新列表中
- java - 实体构造函数给出“不兼容的类型”错误