Re: Using range-based for with alternative ranges
Juha Nieminen <nospam@thanks.invalid> wrote:
It's a bit complicated and verbose, and I was wondering if a simpler
solution could be possible (and also if this could be done with one
single class rather than having to use two).
This is slightly simpler:
//-------------------------------------------------------------------
#include <iterator>
template<typename Iter_t>
struct RangeWrapper
{
Iter_t mBegin, mEnd;
RangeWrapper(Iter_t b, Iter_t e): mBegin(b), mEnd(e) {}
Iter_t begin() { return mBegin; }
Iter_t end() { return mEnd; }
};
template<typename Container_t>
RangeWrapper<typename Container_t::reverse_iterator>
revRange(Container_t& container)
{
return { container.rbegin(), container.rend() };
}
template<typename Elem_t, std::size_t size>
RangeWrapper<std::reverse_iterator<Elem_t*>>
revRange(Elem_t (&array)[size])
{
typedef std::reverse_iterator<Elem_t*> Iter_t;
return { Iter_t(array + size), Iter_t(array) };
}
//-------------------------------------------------------------------