- iterator[meta header]
- std[meta namespace]
- function[meta id-type]
- cpp20[meta cpp]
template<common_with<I> I2>
friend constexpr strong_ordering operator<=>(const counted_iterator& x, const counted_iterator<I2>& y);
- common_with[link /reference/concepts/common_with.md]
- strong_ordering[link /reference/compare/strong_ordering.md]
2つのcounted_iterator
オブジェクトの三方比較を行う。
- (1) :
x, y
はともに同じシーケンス(範囲)についてのイテレータであること。
現在のカウントの値をlength
メンバ変数に保持するとして以下と等価
return y.length <=> x.length;
C++20以降、この演算子により以下の演算子が使用可能になる。
template<common_with<I> I2>
friend constexpr strong_ordering operator<=>(const counted_iterator<I2>& y, const counted_iterator& x);
template<common_with<I> I2>
friend constexpr bool operator<(const counted_iterator& x, const counted_iterator<I2>& y);
template<common_with<I> I2>
friend constexpr bool operator>(const counted_iterator& x, const counted_iterator<I2>& y);
template<common_with<I> I2>
friend constexpr bool operator<=(const counted_iterator& x, const counted_iterator<I2>& y);
template<common_with<I> I2>
friend constexpr bool operator>=(const counted_iterator& x, const counted_iterator<I2>& y);
template<common_with<I> I2>
friend constexpr bool operator<(const counted_iterator<I2>& y, const counted_iterator& x);
template<common_with<I> I2>
friend constexpr bool operator>(const counted_iterator<I2>& y, const counted_iterator& x);
template<common_with<I> I2>
friend constexpr bool operator<=(const counted_iterator<I2>& y, const counted_iterator& x);
template<common_with<I> I2>
friend constexpr bool operator>=(const counted_iterator<I2>& y, const counted_iterator& x);
また、この演算子はHidden friendsとして定義され、使用可能となる演算子もHidden friendsであるかの様に使用可能となる。
#include <iostream>
#include <iterator>
#include <ranges>
#include <vector>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::counted_iterator ci{std::ranges::begin(vec), 5};
auto ci2 = ci + 3;
std::cout << std::boolalpha;
std::cout << (ci <=> ci2 < 0) << '\n';
// <=>から導出される比較演算子
std::cout << (ci < ci2) << '\n';
std::cout << (ci > ci2) << '\n';
std::cout << (ci <= ci2) << '\n';
std::cout << (ci >= ci2) << '\n';
}
- <=>[color ff0000]
- ranges::begin[link /reference/ranges/begin.md]
true
true
false
true
false
- C++20
- Clang: ??
- GCC: 10.1 [mark verified]
- Visual C++: 2019 Update 9 [mark verified]