虽然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这里有两个模板参数,所以我们仍然无法比较两个不同的pair
s, 例如 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 中也支持使用此类迭代器对范围进行排序的比较?这是故意的吗?或者我错过了什么?