C/C++ Arena

Step 5 of 5

Challenge: a ring buffer

A ring buffer (circular buffer) is a fixed-capacity queue stored in an array: the write position wraps around to the start, overwriting the oldest item when full. Audio pipelines, loggers and network stacks use them because they never allocate after construction.

capacity 4, after pushing 1..6:   [5][6][3][4]   oldest = 3
                                          ^head

Your turn: write template <typename T, std::size_t N> class RingBuffer with:

Previous: A lazy range