为什么我的迭代器不是std::input_iterator



为什么下面的迭代器不满足std::input_iterator概念?我错过了什么?

template <class T>
struct IteratorSentinel {};
template <class T>
class Iterator
{
public:
using iterator_category = std::input_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
Iterator() = default;

Iterator(const Iterator&) = delete;
Iterator& operator = (const Iterator&) = delete;
Iterator(Iterator&& other) = default;
Iterator& operator = (Iterator&& other) = default;
T* operator-> ();
T& operator* ();
bool operator== (const IteratorSentinel<T>&) const noexcept;
Iterator& operator++ ();
void operator++ (int);
};

以下静态断言失败:

static_assert(std::input_iterator<Iterator<int>>);

例如,下面的代码不能编译:

template <class T>
auto make_range()
{
return std::ranges::subrange(Iterator<T>(), IteratorSentinel<T>{});
}
std::ranges::equal(make_range<int>(), std::vector<int>());

您的operator*不符合const条件。

std::input_iterator概念(通过std::indirectly_readable概念(要求*可以应用于const和非const的左值以及该类型的右值,并且在所有情况下都返回相同的类型。

最新更新