0
\$\begingroup\$

(This is the Non-const version, I have to implement the const one too). Could someone please review this implementation? This is made for std::vector I'm unsure whether I respected all requirements for LegacyRandomAccessIterator; so if I'm missing something, please do let me know.

namespace random_access
{
    template<typename Type>
    class iterator
    {
    private:
        Type* m_iterator;


    public:
        using value_type = Type;
        using reference = value_type&;
        using pointer = value_type*;
        using iterator_category = std::random_access_iterator_tag;
        using difference_type = std::ptrdiff_t;
        //using iterator_concept = std::contiguous_iterator_tag;

        constexpr iterator(Type* iter = nullptr) : m_iterator{ iter } {}

        constexpr bool operator==(const iterator& other) const noexcept { return m_iterator == other.m_iterator; }
        constexpr bool operator!=(const iterator& other) const noexcept { return m_iterator != other.m_iterator; }
        constexpr reference operator*() const noexcept { return *m_iterator; }
        constexpr pointer operator->() const noexcept { return m_iterator; }
        constexpr iterator& operator++() noexcept { ++m_iterator; return *this; }
        constexpr iterator operator++(int) noexcept { iterator tmp(*this); ++(*this); return tmp; }
        constexpr iterator& operator--() noexcept { --m_iterator; return *this; }
        constexpr iterator operator--(int) noexcept { iterator tmp(*this); --(*this); return tmp; }
        constexpr iterator& operator+=(const difference_type other) noexcept { m_iterator += other; return *this; }
        constexpr iterator& operator-=(const difference_type other) noexcept { m_iterator -= other; return *this; }
        constexpr iterator operator+(const difference_type other) const noexcept { return iterator(m_iterator + other); }
        constexpr iterator operator-(const difference_type other) const noexcept { return iterator(m_iterator - other); }
        constexpr iterator operator+(const iterator& other) const noexcept { return iterator(*this + other.m_iterator); }
        constexpr difference_type operator-(const iterator& other) const noexcept { return std::distance(m_iterator, other.m_iterator); }
        constexpr reference operator[](std::size_t index) const { return m_iterator[index]; }
        constexpr bool operator<(const iterator& other) const noexcept { return m_iterator < other.m_iterator; }
        constexpr bool operator>(const iterator& other) const noexcept { return m_iterator > other.m_iterator; }
        constexpr bool operator<=(const iterator& other) const noexcept { return m_iterator <= other.m_iterator; }
        constexpr bool operator>=(const iterator& other) const noexcept { return m_iterator >= other.m_iterator; }
    };
}

Thanks !

\$\endgroup\$
3
  • \$\begingroup\$ Can I ask what is the purpose of this class? \$\endgroup\$ – slepic Jan 12 at 11:38
  • \$\begingroup\$ A pointer already implements the "Random Access Iterator" concept. You don't need to wrap it in a class. \$\endgroup\$ – Martin York Jan 12 at 18:01
  • \$\begingroup\$ What does it mean to add two iterators together? iterator operator+(const iterator& other) That does not seem to have any meaning. \$\endgroup\$ – Martin York Jan 12 at 18:04
0
\$\begingroup\$

As pointed out by @Martin York in the comments that your class is just a wrapper for a pointer, I don't have much to say. I only have one suggestion

  • Better Formatting

Good formatting IMO plays a major role in how readable your code is. In this case, I find it impossible to navigate through the functions because they all look extremely cramped.

constexpr pointer operator->() const noexcept { return m_iterator; }
constexpr iterator& operator++() noexcept { ++m_iterator; return *this; }

Compare that to

constexpr pointer operator->() const noexcept { 
    return m_iterator;
}

constexpr iterator& operator++() noexcept { 
    ++m_iterator; 
    return *this; 
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.