C++ PATTERNS

C++ Container With Conditionally Protected Access

Design proxy-based indexator and iterator for your container

Pavlo Horbonos
The Startup
Published in
4 min readFeb 13, 2020

--

It is the second issue of the series about the practical C++ designs. The last time we have discussed the creation of an accumulator container (check it here). Today let’s have a talk about the access and iterating. I want to share with you an interesting pattern I have used in one of my projects.

When we talk about the processing of the container’s content, we mean two situations. The first one is an iterator with full access to read and change the elements of the container. The second one is the constant iterator which has no right to change the content. Though what if we need to change the value of an element, but have to keep some condition on it?

Let’s see an example. We have a simple array-based container, which should keep only values from the interval [0, 1]. If we start with the simplest form of changing the content — index access operator (or random-access operator or indexator), at some moment we have to watch the next picture:

--

--

Pavlo Horbonos
The Startup

Art & Code | https://twitter.com/MidvelCorp 💻| Head of Blockchain Security in https://blaize.tech/ | web3 and blockchain enthusiast and researcher