namespace std {
template <class Iterator>
class move_iterator {
public:
template <sentinel_for<Iterator> S>
friend constexpr bool operator==(const move_iterator& x,
const move_sentinel<S>& y); // (1) C++20
// (1)のoperator==により、以下のオーバーロードが使用可能になる (C++20)
template <sentinel_for<Iterator> S>
friend constexpr bool operator==(const move_sentinel<S>& x,
const move_iterator& y); // (2) C++20
};
template <class Iterator1, class Iterator2>
bool operator==(const move_iterator<Iterator1>& x,
const move_iterator<Iterator2>& y); // (3) C++11
template <class Iterator1, class Iterator2>
constexpr bool operator==(const move_iterator<Iterator1>& x,
const move_iterator<Iterator2>& y); // (3) C++17
}
概要
2つのmove_iterator
オブジェクトが同じ要素を指しているかを判定する。
テンプレートパラメータ制約
x.base() == y.base()
が有効であり、戻り値がbool
に変換可能であること。
戻り値
return x.base() == y.base();
x.base()
とy.base()
はオーバーロードによって次のどちらか
備考
C++20以降、これらの演算子により以下の演算子が使用可能になる。
operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y)
operator!=(const move_iterator& x, const move_sentinel<S>& y)
operator!=(const move_sentinel<S>& x, const move_iterator& y)
例
#include <iostream>
#include <vector>
#include <memory>
#include <iterator>
int main()
{
std::vector<std::unique_ptr<int>> v;
for (int i = 0; i < 5; ++i)
v.emplace_back(new int(i));
auto it1 = std::make_move_iterator(v.begin());
auto it2 = std::make_move_iterator(v.begin());
if (it1 == it2) {
std::cout << "equal" << std::endl;
}
else {
std::cout << "not equal" << std::endl;
}
}
出力
equal
バージョン
言語
- C++11
処理系
- Clang: ??
- GCC: 4.7.0 ✅
- ICC: ??
- Visual C++: ??