I’d like to wrap std::array to create a circular array but how to provide aggregate initialization

With std::array you can do

std::array<int,3> v = {1,2,3};

But I want to provide a wrapper

template <typename T, int n> circular_array {
   auto operator[](size_t i){
       return m_data[i%n];  
   }
   // TODO forward all other methods directly except ``at`` which we don't need
 private:
   std::array<t,n> m_data;
}

but I lose the aggregate initialization that I had above for std::array. This will not work.

circular_array<int,3> v = {1,2,3};

though if I made m_data public I could do

circular_array<int,3> v = {{1,2,3}};

which is not desired.

Is there a way to achieve circular_array as a drop in for std::array including aggregate initialization?

Source: Windows Questions C++

LEAVE A COMMENT