Peregring-lk Peregring-lk - 3 years ago 146
C++ Question

Iterator category of std::move_iterator

Does it make sense that the

std::move_iterator
adaptor just inherits the iterator category of the template parameter Iterator type?

Because forward iterators requires that the reference type is of cv-
T&
, however, for forward iterator templates, the
std::move_iterator<It>::reference
will be an r-value reference to the Iterator's value type (with the reference qualifiers preserved).

Answer Source

Because forward iterators requires that the reference type is of cv-T&, however

Incorrect. The standard (C++17, N4659, [forward.iterators]/1.3) says:

if X is a mutable iterator, reference is a reference to T ; if X is a constant iterator, reference is a reference to const T

Rvalue references are a "reference to T". Therefore, they qualify. If they meant to exclude rvalue references, the standard would have said "lvalue reference to T".

So it's perfectly valid for a ForwardIterator to return an rvalue reference.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download