3

看起来我有点简单,因为我不太能够在下面标记为错误的行上清楚地看到此错误的原因。

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;
}
4

1 回答 1

3

Casey 通过range-v3 问题列表快速回答。

这是他的评论,作为要求的文本,而不是我放在这里的原始图像:

ranges::sort如果没有比较器参数,则需要对类型进行排序以对StrictTotallyOrdered概念进行建模。这意味着类型必须定义所有==, !=, <, >, <=, 和>=具有一致的语义。

我在那里回复:

谢谢你这么快就回来了。我现在知道了。我必须说它std::sortboost::sort要求不兼容有点令人失望。range-v3 我猜这就是我们为可爱付出的代价。

再次感谢,--马特。

不幸的是,要求高于std::sortboost::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));      \
  } 
于 2018-06-24T15:59:36.157 回答