range::sort 无法编译

2024-02-24

在 MyRect.h 中:

struct MyRect
{
    MyRect(std::initializer_list<int> i);
    MyRect();
    int16_t m_left=0, m_right=0, m_top=0, m_bottom=0 ;
    int16_t no_sequence=0 ;
    int16_t i=-1 ;
    bool selected=false ;
} ;

bool operator==(const MyRect& r1, const MyRect& r2) ;
bool operator<(const MyRect& r1, const MyRect& r2);

在 MyRect.cpp 中:

bool operator==(const MyRect& r1, const MyRect& r2)
{
    return r1.m_left==r2.m_left &&
        r1.m_right==r2.m_right &&
        r1.m_top==r2.m_top &&
        r1.m_bottom==r2.m_bottom ;
}
bool operator<(const MyRect& r1, const MyRect& r2)
{
    if (r1.m_left != r2.m_left)
        return r1.m_left < r2.m_left;
    if (r1.m_right != r2.m_right)
        return r1.m_right < r2.m_right;
    if (r1.m_top != r2.m_top)
        return r1.m_top < r2.m_top;
    if (r1.m_bottom != r2.m_bottom)
        return r1.m_bottom < r2.m_bottom;
    //if we got here, r1==r2
    return false;
}

在 binpack.cpp 中:

#include "MyRect.h"
...
vector<MyRect> selected_neighboors ;
std::sort(selected_neighboors.begin(), selected_neighboors.end()) ;

使用 g++10 (C++20) binpack.cpp 进行编译。没有问题。

如果我改为:

ranges::sort(selected_neighboors);

它不再编译。

/home/edouda/linkedboxdraw/binpack.cpp: In function ‘void collapse(std::vector<MyRect>&)’:
/home/edouda/linkedboxdraw/binpack.cpp:617:36: error: no match for call to ‘(const std::ranges::__sort_fn) (std::vector<MyRect>&)’
  617 |    ranges::sort(selected_neighboors) ;
      |                                    ^
In file included from /usr/include/c++/10/algorithm:64,
                 from /home/edouda/linkedboxdraw/MyRect.h:14,
                 from /home/edouda/linkedboxdraw/binpack.h:12,
                 from /home/edouda/linkedboxdraw/binpack.cpp:8:
/usr/include/c++/10/bits/ranges_algo.h:2019:7: note: candidate: ‘template<class _Iter, class _Sent, class _Comp, class _Proj>  requires (random_access_iterator<_Iter>) && (sentinel_for<_Sent, _Iter>) && (sortable<_Iter, _Comp, _Proj>) constexpr _Iter std::ranges::__sort_fn::operator()(_Iter, _Sent, _Comp, _Proj) const’
 2019 |       operator()(_Iter __first, _Sent __last,
      |       ^~~~~~~~
/usr/include/c++/10/bits/ranges_algo.h:2019:7: note:   template argument deduction/substitution failed:
/home/edouda/linkedboxdraw/binpack.cpp:617:36: note:   candidate expects 4 arguments, 1 provided
  617 |    ranges::sort(selected_neighboors) ;
      |                                    ^
In file included from /usr/include/c++/10/algorithm:64,
                 from /home/edouda/linkedboxdraw/MyRect.h:14,
                 from /home/edouda/linkedboxdraw/binpack.h:12,
                 from /home/edouda/linkedboxdraw/binpack.cpp:8:
/usr/include/c++/10/bits/ranges_algo.h:2032:7: note: candidate: ‘constexpr std::ranges::borrowed_iterator_t<_Range> std::ranges::__sort_fn::operator()(_Range&&, _Comp, _Proj) const [with _Range = std::vector<MyRect>&; _Comp = std::ranges::less; _Proj = std::identity; std::ranges::borrowed_iterator_t<_Range> = std::conditional<true, __gnu_cxx::__normal_iterator<MyRect*, std::vector<MyRect> >, std::ranges::dangling>::type]’
 2032 |       operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const
      |       ^~~~~~~~
/usr/include/c++/10/bits/ranges_algo.h:2032:7: note: constraints not satisfied
In file included from /usr/include/c++/10/compare:39,
                 from /usr/include/c++/10/bits/stl_pair.h:65,
                 from /usr/include/c++/10/bits/stl_algobase.h:64,
                 from /usr/include/c++/10/vector:60,
                 from /home/edouda/linkedboxdraw/MyRect.h:12,
                 from /home/edouda/linkedboxdraw/binpack.h:12,
                 from /home/edouda/linkedboxdraw/binpack.cpp:8:
/usr/include/c++/10/concepts: In instantiation of ‘constexpr std::ranges::borrowed_iterator_t<_Range> std::ranges::__sort_fn::operator()(_Range&&, _Comp, _Proj) const [with _Range = std::vector<MyRect>&; _Comp = std::ranges::less; _Proj = std::identity; std::ranges::borrowed_iterator_t<_Range> = std::conditional<true, __gnu_cxx::__normal_iterator<MyRect*, std::vector<MyRect> >, std::ranges::dangling>::type]’:
/home/edouda/linkedboxdraw/binpack.cpp:617:36:   required from here
/usr/include/c++/10/concepts:338:13:   required for the satisfaction of ‘invocable<_Fn, _Args ...>’ [with _Fn = std::ranges::less&; _Args = {value_type<MyRect>&, value_type<MyRect>&}]
/usr/include/c++/10/concepts:342:13:   required for the satisfaction of ‘regular_invocable<_Fn, _Args ...>’ [with _Fn = std::ranges::less&; _Args = {value_type<MyRect>&, value_type<MyRect>&}]
/usr/include/c++/10/concepts:346:13:   required for the satisfaction of ‘predicate<_Rel, _Tp, _Tp>’ [with _Rel = std::ranges::less&; _Tp = MyRect&]
/usr/include/c++/10/concepts:351:13:   required for the satisfaction of ‘relation<_Rel, _Tp, _Up>’ [with _Rel = std::ranges::less&; _Tp = MyRect&; _Up = MyRect&]
/usr/include/c++/10/concepts:361:13:   required for the satisfaction of ‘strict_weak_order<_Fn&, typename std::__detail::__iter_traits_impl<typename std::remove_cv<typename std::remove_reference<_Arg>::type>::type, std::indirectly_readable_traits<typename std::remove_cv<typename std::remove_reference<_Arg>::type>::type> >::type::value_type&, typename std::__detail::__iter_traits_impl<typename std::remove_cv<typename std::remove_reference<_Arg>::type>::type, std::indirectly_readable_traits<typename std::remove_cv<typename std::remove_reference<_Arg>::type>::type> >::type::value_type&>’ [with _Fn = std::ranges::less; _Arg = std::projected<__gnu_cxx::__normal_iterator<MyRect*, std::vector<MyRect, std::allocator<MyRect> > >, std::identity>; _Arg = std::projected<__gnu_cxx::__normal_iterator<MyRect*, std::vector<MyRect, std::allocator<MyRect> > >, std::identity>]
/usr/include/c++/10/bits/iterator_concepts.h:690:13:   required for the satisfaction of ‘indirect_strict_weak_order<_Rel, std::projected<_Iter, _Proj>, std::projected<_Iter, _Proj> >’ [with _Rel = std::ranges::less; _Iter = __gnu_cxx::__normal_iterator<MyRect*, std::vector<MyRect, std::allocator<MyRect> > >; _Proj = std::identity]
/usr/include/c++/10/bits/iterator_concepts.h:865:13:   required for the satisfaction of ‘sortable<decltype (std::__detail::__ranges_begin(declval<_Container&>())), _Comp, _Proj>’ [with _Container = std::vector<MyRect, std::allocator<MyRect> >&; _Comp = std::ranges::less; _Proj = std::identity]
/usr/include/c++/10/concepts:338:25: note: the expression ‘is_invocable_v<_Fn, _Args ...> [with _Fn = std::ranges::less&; _Args = {value_type<MyRect>&, value_type<MyRect>&}]’ evaluated to ‘false’
  338 |     concept invocable = is_invocable_v<_Fn, _Args...>;
      |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/latuile.dir/build.make:76: CMakeFiles/latuile.dir/binpack.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/latuile.dir/all] Error 2
make: *** [Makefile:95: all] Error 2

ranges::sort uses ranges::less比较MyRect默认情况下,其定义在[范围.cmp] https://eel.is/c++draft/range.cmp#8:

struct ranges::less {
  template<class T, class U>
    constexpr bool operator()(T&& t, U&& u) const;

  using is_transparent = unspecified;
};
template<class T, class U>
  constexpr bool operator()(T&& t, U&& u) const;

约束条件: T and U满足totally_­ordered_­with.

Its operator()要求T and U必须满足total_ordered_with https://eel.is/c++draft/concept.totallyordered#concept:totally_ordered_with:

template<class T, class U>
  concept totally_­ordered_­with =
    totally_­ordered<T> && totally_­ordered<U> && ...

这要求T必须满足totally_ordered https://eel.is/c++draft/concept.totallyordered#concept:totally_ordered:

template<class T>
  concept totally_­ordered =
    equality_­comparable<T> && partially-ordered-with<T, T>;

这要求T必须满足partially-ordered-with https://eel.is/c++draft/cmp.concept#concept:partially-ordered-with:

template<class T, class U>
  concept partially-ordered-with =      // exposition only
    requires(const remove_reference_t<T>& t, const remove_reference_t<U>& u) {
      { t <  u } -> boolean-testable;
      { t >  u } -> boolean-testable;
      { t <= u } -> boolean-testable;
      { t >= u } -> boolean-testable;
      { u <  t } -> boolean-testable;
      { u >  t } -> boolean-testable;
      { u <= t } -> boolean-testable;
      { u >= t } -> boolean-testable;
    };

这要求完整的关系运算符集必须是格式良好的。

由于您没有定义合适的operator>, operator<=和其他关系运算符MyRect,不满足约束条件。

你可以加operator<=> to MyRect做到这一点totally_ordered,或者使用无约束的std::less进行比较:

std::vector<MyRect> selected_neighboors;
ranges::sort(selected_neighboors, std::less{});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

range::sort 无法编译 的相关文章

随机推荐

  • 如何在 Xcode 7 中使用 xcodebuild 和手表扩展

    我们的命令以前是这样的 xcodebuild configuration Release target xxx sdk iphoneos9 0 scheme xxx archive 现在在 Xcode 7 中 我们收到此错误 Build s
  • 通过 Linux FrameBuffer 将像素绘制到屏幕

    最近 我被一个奇怪的想法所震惊 从 dev urandom 获取输入 将相关字符转换为随机整数 然后使用这些整数作为像素的 rgb x y 值来绘制到屏幕上 我做了一些研究 在 StackOverflow 和其他地方 许多人建议您可以直接写
  • 使用 iText 更改 PDF Producer 属性

    是否可以使用 iText 更改 PDF 文档的现有属性 我试过这个 Map
  • Google 登录错误 12500

    我正在尝试将 Google Sign In 集成到我的应用程序中 我没有后端服务器 我只是将登录的 Google 帐户的详细信息获取到我的应用程序 我首先尝试使用Google 登录示例 https github com googlesamp
  • 转义特殊字符 (ø, æ) 以在 url 内使用

    我尝试在 iOS 应用程序中显示带有 URL 源的图像 但它没有显示 图像的 url 是实时示例路径 使用以下 Objective C 代码转义该字符串时 NSString url NSString CFURLCreateStringByA
  • 检查指定名称的屏幕是否存在

    我制作了一个 bash 文件 它在独立的屏幕中启动另一个具有唯一名称的 bash 文件 我需要确保该内部 bash 文件的一个实例在任何一个时间点都在运行 为此 我希望在尝试创建父 bash 文件之前检查该名称的屏幕是否存在 有没有办法做到
  • 正则表达式 $1、$2 等

    我一直在尝试在 PHP 中进行一些正则表达式操作 但我在这方面不是很熟练 似乎当我在字符串上使用像 preg replace 这样的正则表达式函数时 我可以通过某种名为 1 2 等的变量访问正则表达式替换的字符串 这叫什么 我该如何使用它
  • 使 CSS 工具提示跟随光标

    我正在创建一个基于 CSS 的工具提示 该工具提示中将包含大量内容 而不是处于静态位置 我想知道是否有一种简单的方法可以使其在将鼠标悬停在链接上时跟随光标 这是基于 CSS 的工具提示示例 div class couponcode Firs
  • Flux/React Complex 可重复使用组件

    我想做这样的事情 var App React createClass render function return
  • 如何在Django中获取所有POST请求值?

    有没有办法从 Django 的请求中获取所有表单名称
  • 为什么 TextRenderer.MeasureText 无法正常工作?

    我想在给定一定宽度的可用画布的情况下测量文本的高度 我传入的文本非常长 我知道会换行 为此 我呼吁如下 using System Windows Forms string text Really really long text that
  • 如何在使用清单 v3 制作的 Chrome 扩展中使用 Web 套接字显示实时数据?

    我正在开发一个简单的扩展 我想在其中展示RealTime通过网络套接字连接从我的服务器发送的数据 我正在使用 chrome 的清单 v3 来实现这一点 我尝试通过网络套接字连接我的扩展background js它作为 Service Wor
  • std::map 键的要求(设计决策)

    当我做一个std map
  • 协调 CACurrentMediaTime() 和 deviceCurrentTime

    我正在尝试同步几个CABasicAnimations with AVAudioPlayer 我的问题是CABasicAnimation uses CACurrentMediaTime 作为安排动画时的参考点AVAudioPlayer use
  • Python argparse:有没有办法在 nargs 中指定范围?

    我有一个可选参数 它支持参数列表本身 我的意思是 它应该支持 f 1 2 f 1 2 3 but not f 1 f 1 2 3 4 有没有办法在 argparse 中强制执行此操作 现在我使用 nargs 然后检查列表长度 Edit 根据
  • C++ 中的私有成员与临时变量

    假设您有以下代码 int main int argc char argv Foo f while true f doSomething 以下两种 Foo 实现中哪一个是首选 解决方案一 class Foo private void doIt
  • LINQ FirstOrDefault 检查默认值

    如何检查是否返回了对象FirstOrDefaultLINQ功能其实是默认的 例如 Contact contact dbo contact Where m gt m contactName Stackoverflow FirstOrDefau
  • 如何在flutter中创建工具栏搜索视图

    我需要实施searchview在工具栏中我的应用程序用于过滤列表视图 在 aziza回答的帮助下 我用下面的列表过滤器编写了搜索视图的详细代码片段 这对其他人有帮助 import package flutter material dart
  • SQL SERVER MANAGEMENT STUDIO:突出显示变量

    SQL SERVER MANAGEMENT STUDIO 中有没有一种方法可以在我们单击变量名称时突出显示变量的所有用法 它可能不适用于所有情况 但我发现使用F2按钮 在我的机器上映射到 View EditLabel 它使我能够突出显示所有
  • range::sort 无法编译

    在 MyRect h 中 struct MyRect MyRect std initializer list