在 C++23 中,仍然无法使用 value_type/reference 为pair的代理迭代器对范围进行排序吗?

2023-12-21

虽然P2321 zip https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2321r2.html#pair提供了common_reference https://stackoverflow.com/questions/59011331/what-is-the-purpose-of-c20-stdcommon-reference专业化pair, and P2165 之间的兼容性tuple, pair and tuple类物体 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2165r4.pdf makes tuple and pair可相互转换,比较函数pair只有以下候选人[对规格] https://eel.is/c++draft/pairs.spec:

template<class T1, class T2>
  constexpr common_comparison_category_t<...>
    operator<=>(const pair<T1, T2>& x, const pair<T1, T2>& y);

注意到有only这里有两个模板参数,所以我们仍然无法比较两个不同的pairs, 例如 https://godbolt.org/z/T6ov9K5K4:

using value_type = pair<int , string >;
using reference  = pair<int&, string&>;

value_type val = {1, "a"};
reference  ref = val;
val < ref; // no match for 'operator<'

这意味着代理迭代器使用pair as value_type/reference总是无法满足sortable https://en.cppreference.com/w/cpp/iterator/sortable的概念,即iter_value_t<I>必须与iter_reference_t<I>,这表明ranges::sort对于这样一类迭代器不起作用(例如zip_view::iterator before P2165 https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2165r4.pdf) 除非手动传入自定义比较器:

std::vector<int> x, y;
ranges::sort(std::views::zip(x, y)); // This won't work before P2165 
 // because the reference type of zip_view is pair<int&, int&>,
 // which cannot be compared with its value type pair<int, int>.

 // After P2165 its reference type was changed to tuple<int&, int&>, 
 // which works because it can be compared with its value type tuple<int, int>.

Why doesn't标准引入异构pair即使在 C++23 中也支持使用此类迭代器对范围进行排序的比较?这是故意的吗?或者我错过了什么?


为什么标准不引入异构pair即使在 C++23 中也支持使用此类迭代器对范围进行排序的比较?

LWG-3865“对一系列pairs" https://cplusplus.github.io/LWG/issue3865正是这样做的。它最近在 2023-02 WG21 会议上被合并到 C++23 工作草案中。 (“最近”就像“在撰写本文时更新的工作草案尚未完成”,因此如果您在最新的草案中找不到它,请不要感到惊讶。)

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

在 C++23 中,仍然无法使用 value_type/reference 为pair的代理迭代器对范围进行排序吗? 的相关文章

随机推荐