Reverse C++11 range-based for loop I wrote this snippet:

template &lt;typename ContainerT&gt;
class ReverseIterator
{
public:
    ReverseIterator(ContainerT&amp; iContainer) : m_container{iContainer}
    {

    }

    typename ContainerT::reverse_iterator begin()
    {
        return m_container.rbegin();
    }

    typename ContainerT::reverse_iterator end()
    {
        return m_container.rend();
    }

private:
    ContainerT&amp; m_container;
};

template &lt;typename ContainerT&gt;
auto Reverse(ContainerT&amp; iContainer)
{
    return ReverseIterator&lt;ContainerT&gt;(iContainer);
}


To be able to do something like this:

std::vector&lt;int&gt; myList{1, 2, 3};
for (auto&amp; itr : Reverse(myList))
{
    ++itr;
}


First I'd like a feedback up to this point.

Then, I'd like to be able to write something like this:

for (auto itr : Reverse(std::vector&lt;int&gt;{1, 2, 3}))
{
    std::cout &lt;&lt; itr &lt;&lt; " ";
}


Without having a separate and near identical implementation of ConstReverse.

I thought about making everything with forwarding references but for some reason that didn't work out very well and I'm not exactly sure where I was mistaken. Could someone show me how it could be done?
Good clear template code - well done!

I'd consider changing the name, as the class itself isn't an iterator, it's an iterator pair, or range, that wraps a container and provides a reversed view of it.

Constructor should be explicit.

We should use free-function rbegin() and rend() to give us greater flexibility of container types (e.g. C-style arrays) - fall back to the std versions in &lt;iterator&gt; if argument-dependent lookup fails.

It could be improved by overloading Reverse with a version that accepts a const Container:

template &lt;typename ContainerT&gt;
auto Reverse(const ContainerT&amp; iContainer)
{
    return ReverseIterator&lt;const ContainerT&gt;(iContainer);
}


For this to work properly, we need to change the return type of begin() and end() to auto, so that we get const_reverse_iterator back from them.  If you're really stuck with C++11, then we'll have to add the trailing return type: auto begin()-&gt; decltype(m_container.begin()), for example.  For my own convenience, I'll work with C++17 from here onwards.



Applying these suggested changes gives us:

template &lt;typename ContainerT&gt;
class ReverseContainerView
{
public:
    explicit ReverseContainerView(ContainerT&amp; iContainer)
      : m_container{iContainer}
    {
    }

    auto begin() {
        using std::rbegin;
        return rbegin(m_container);
    }
    auto end() {
        using std::rend;
        return rend(m_container);
    }

private:
    ContainerT&amp;  m_container;
};


template&lt;typename ContainerT&gt;
auto Reverse(ContainerT&amp; iContainer)
{
    return ReverseContainerView&lt;ContainerT&gt;(iContainer);
}

template&lt;typename ContainerT&gt;
auto Reverse(const ContainerT&amp; iContainer)
{
    return ReverseContainerView&lt;const ContainerT&gt;(iContainer);
}


and demo:

#include &lt;iostream&gt;
#include &lt;vector&gt;
int main()
{
    auto list = std::vector&lt;int&gt;{1, 2, 3};
    for (auto&amp; i: Reverse(list)) {
        std::cout &lt;&lt; ++i &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';
    for (const auto&amp; i: list) {
        std::cout &lt;&lt; i &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';

    const auto const_list = std::vector&lt;int&gt;{1, 2, 3};
    for (auto i: Reverse(const_list)) {
        std::cout &lt;&lt; i &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';
}




Note that the proposed usage for (auto i: Reverse(std::vector&lt;int&gt;{10, 20, 30})) (where the constructor argument is an xvalue) won't work with the above code.  That's because the temporary has gone out of scope after the ReverseContainer was constructed but before the for loop completes, leaving a dangling reference in the container.  We'll need a version that captures and stores any rvalue type passed to it.

The easiest way I know to do this is to split the class into its two responsibilities: a base class to capture the wrapped container and a subclass to access the reversed view (we might want to put some or all of these classes into an inner namespace to hide them from users; I'll leave that question open here).

#include &lt;iterator&gt;
#include &lt;type_traits&gt;
#include &lt;utility&gt;

template &lt;typename T, bool CopyValue = !std::is_lvalue_reference_v&lt;T&gt;&gt;
struct ContainerContainer;

template &lt;typename T&gt;
struct ContainerContainer&lt;T, false&gt;
{
    // lvalue version: contains a reference to the container
    T&amp; container;
    explicit ContainerContainer(T&amp; container)
      : container{container}
    {}
};

template &lt;typename T&gt;
struct ContainerContainer&lt;T, true&gt;
{
    // rvalue version: move-constructs a new copy of container
    const T container;
    explicit ContainerContainer(T container)
      : container{std::move(container)}
    {}
};




template &lt;typename Container&gt;
class ReverseContainerView : ContainerContainer&lt;Container&gt;
{
    using Base = ContainerContainer&lt;Container&gt;;
public:
    explicit ReverseContainerView(Container&amp;&amp; container)
        : Base{std::forward&lt;Container&gt;(container)}
    {}

    auto begin() {
        using std::rbegin;
        return rbegin(Base::container);
    }
    auto end() {
        using std::rend;
        return rend(Base::container);
    }
};


template&lt;typename Container&gt;
auto Reverse(Container&amp;&amp; container)
{
    return ReverseContainerView&lt;Container&gt;(std::forward&lt;Container&gt;(container));
}




// test program
#include &lt;iostream&gt;
#include &lt;vector&gt;
int main()
{
    int list[] = {1, 2, 3};
    for (auto&amp; i: Reverse(list)) {
        std::cout &lt;&lt; ++i &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';

    const auto const_list = {1, 2, 3}; // initializer_list
    for (auto&amp; i: Reverse(const_list)) {
        // ++i; ERROR: 'i' is read-only
        std::cout &lt;&lt; i &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';

    for (auto&amp; i: Reverse(std::vector&lt;int&gt;{10, 20, 30})) {
        // ++i; ERROR: 'i' is read-only
        std::cout &lt;&lt; i &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';
}




From C++17 onwards, we don't really need the Reverse() helper function: we can instead simply provide a template deduction guide:

template&lt;typename Container&gt;
explicit ReverseContainerView(Container&amp;&amp; container)
    -&gt; ReverseContainerView&lt;Container&gt;;


We might want to choose a shorter name for the class now that it's more visible to the users.