Rotation view for std::vector I have this view class for constant time rotations of a std::vector:

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;

template&lt;typename T&gt;
class vector_view {

public:
    vector_view(std::vector&lt;T&gt;&amp; vec) : vec{&amp;vec}, offset{0} {}
    const T&amp; operator[](int index) const {
        return (*vec)[(index + offset) % vec-&gt;size()];
    }

    T&amp; operator[](int index) {
        return (*vec)[(index + offset) % vec-&gt;size()];
    }

    void rotate(int rotation_length) {
        offset = (offset + vec-&gt;size() - rotation_length) % vec-&gt;size();
    }

    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, vector_view&lt;T&gt;&amp; view) {
        os &lt;&lt; "[";
        std::string separator = "";

        for (int index = 0; index &lt; view.vec-&gt;size(); ++index)
        {
            os &lt;&lt; separator &lt;&lt; view[index];
            separator = ", ";
        }

        return os &lt;&lt; "]";
    }

private:
    std::vector&lt;T&gt;* vec;
    int offset;
};

int main() {
    std::vector&lt;char&gt; v{'a', 'b', 'c', 'd', 'e'};
    vector_view&lt;char&gt; view{v};

    for (int i = 0; i &lt; v.size(); ++i) {
        std::cout &lt;&lt; view &lt;&lt; std::endl;
        view.rotate(1);
    }

    std::cout &lt;&lt; std::endl;

    for (int i = 0; i &lt; v.size(); ++i) {
        std::cout &lt;&lt; view &lt;&lt; std::endl;
        view.rotate(-1);
    }
}


The demo output is:



[a, b, c, d, e]
[e, a, b, c, d]
[d, e, a, b, c]
[c, d, e, a, b]
[b, c, d, e, a]

[a, b, c, d, e]
[b, c, d, e, a]
[c, d, e, a, b]
[d, e, a, b, c]
[e, a, b, c, d]



Since I am still in the process of learning modern C++, please tell me anything I could improve here; such as:


Naming,
coding conventions,
API,
...
I'm assuming that you want your vector elements to be modifiable through this view, as vec is non-const.
You store a pointer to the underlying vector, but never test it isn't null.  I think it would be better to have it as a reference member.
If you want your view to be as usable as the standard vector, you'll want to provide iterators, and begin() and end() to obtain them.  You may also want implement front(), back() and at().  Methods such as size(), reserve, capacity etc. can simply forward to the contained vector.

Like std::vector, you should override operator[] on const:
const T&amp; operator[](int index) const {
    return (*vec)[(index + offset) % vec-&gt;size()];
}

T&amp; operator[](int index) {
    return (*vec)[(index + offset) % vec-&gt;size()];
}


Once we've implemented size() or begin()/end(), the streaming operator &lt;&lt; uses only the public interface, so it no longer needs to be friend.
Its loop compares (signed) int against (unsigned) size_t (g++ warns about this with -Wall).  Prefer to avoid implicit promotions, so declare i as size_type to match size().  If you're pedantic, that should be vector_view&lt;T&gt;::size_type, which you'll forward from std::vector&lt;T&gt;.  My code below avoids that entirely, by using iterators.

Beware the gotcha when T is bool.  The only thing preventing your code  working on vector&lt;bool&gt; is:
T&amp; operator[](int index);

If you return a std::vector&lt;T&gt;::reference instead, that will do the right thing there.  (std::vector&lt;bool&gt;::reference is a proxy object, not a bool&amp;).
My modified class:
#include &lt;iterator&gt;
#include &lt;vector&gt;

template&lt;typename T&gt;
class vector_view {
    using vector_type = std::vector&lt;T&gt;;

public:
    using value_type = typename vector_type::value_type;
    using allocator_type = typename vector_type::allocator_type;
    using size_type = typename vector_type::size_type;
    using difference_type = typename vector_type::difference_type;
    using reference = typename vector_type::reference;
    using const_reference = typename vector_type::const_reference;
    using pointer = typename vector_type::pointer;
    using const_pointer = typename vector_type::const_pointer;

    // Iterators: I've implemented little more than the minimum to
    // make the test program work.  You could expand this, and also
    // provide a specialization of std::iterator_traits&lt;&gt; for each
    // one.
    struct iterator {
        vector_view&amp; v;
        size_type i;
        iterator&amp; operator++() { ++i; return *this;}
        iterator&amp; operator-- () { --i; return *this;}
        reference operator*() const { return v[i]; }
        bool operator!=(const iterator other) const
            { return &amp;v != &amp;other.v || i != other.i;  }
    };

    struct const_iterator {
        const vector_view&amp; v;
        size_type i;
        const_iterator&amp; operator++() { ++i; return *this;}
        const_iterator&amp; operator-- () { --i; return *this;}
        const_reference operator*() const { return v[i]; }
        bool operator!=(const const_iterator other) const
            { return &amp;v != &amp;other.v || i != other.i;  }
    };

    using reverse_iterator = std::reverse_iterator&lt;iterator&gt;;
    using const_reverse_iterator = std::reverse_iterator&lt;const_iterator&gt;;

    explicit vector_view(vector_type&amp; vec, int offset = 0)
        : vec{vec},
          offset{offset}
    {}

    const reference operator[](size_type index) const {
        return vec[(index + offset) % vec.size()];
    }

    reference operator[](size_type index) {
        return vec[(index + offset) % vec.size()];
    }

    void rotate(int rotation_length) {
        auto size = vec.size();
        offset = (offset + size - rotation_length) % size;
    }

    void set_rotation(int rotation_length) {
        offset = rotation_length % vec.size();
    }

    int rotation() const {
        return offset;
    }

    // Iterators
    iterator begin() {
        return iterator{*this, 0};
    }
    const_iterator begin() const {
        return const_iterator{*this, 0};
    }
    const_iterator cbegin() const {
        return begin();
    }
    iterator end() {
        return iterator{*this, size()};
    }
    const_iterator end() const {
        return const_iterator{*this, size()};
    }
    const_iterator cend() const {
        return end();
    }
    // You may wish to implement rbegin/rend and crbegin/crend

    bool empty() const { return vec.empty(); }
    size_type size() const { return vec.size(); }

    // You may also want:
    // reserve(), capacity()
    // clear(), insert(), emplace(), erase(), ..., etc.

private:
    vector_type&amp; vec;
    int offset;
};


// Consider implementing operator==(), operator&lt;() and other
// comparisons (as non-member functions, probably).  You could
// probably use std::mismatch() for that.


// Printing: greatly simplified using range-based `for`.
#include &lt;iostream&gt;
#include &lt;string&gt;
template&lt;typename T&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const vector_view&lt;T&gt;&amp; view) {
    os &lt;&lt; &quot;[&quot;;

    std::string separator = &quot;&quot;;
    for (auto element: view) {
        os &lt;&lt; separator &lt;&lt; element;
        separator = &quot;, &quot;;
    }

    return os &lt;&lt; &quot;]&quot;;
}


// Test program: exercises both `normal` and boolean vectors.
int main() {
    std::vector&lt;char&gt; v{'a', 'b', 'c', 'd', 'e'};
    vector_view&lt;char&gt; view{v};

    for (size_t i = 0; i &lt; view.size(); ++i) {
        std::cout &lt;&lt; view &lt;&lt; std::endl;
        view.rotate(1);
    }
    std::cout &lt;&lt; std::endl;

    for (size_t i = 0; i &lt; view.size(); ++i) {
        std::cout &lt;&lt; view &lt;&lt; std::endl;
        view.rotate(-1);
    }
    std::cout &lt;&lt; std::endl;

    std::vector&lt;bool&gt; bvec{{false, true, true, false, false}};
    auto bview = vector_view&lt;bool&gt;{bvec};

    for (size_t i = 0; i &lt; bview.size(); ++i) {
        bview.set_rotation(i);
        std::cout &lt;&lt; bview &lt;&lt; std::endl;
    }
}