New answers tagged

4

Design review Your design is mostly sound. It can be simplified a little bit, and optionally made a bit more rigorous, but the basic design is not bad. The name for what you’re doing is the “Policy” design pattern. The gist is that you create a class templated on policy types, and then concrete policy classes to control the behaviour of the main class. Even ...


3

You are asking how you can guard against invalid iterators; I assume you are most concerned about iterators that have become invalid after the vector had to re-allocate when it outgrew its capacity. One solution is to store an index instead of an iterator; as long as the vector doesn't shrink the range will be valid, and its validity can be trivially checked ...


6

You are implementing std::span What you are trying to implement is basically C++20's std::span. It might be worthwhile to look at how it is implemented, and copy its interface if possible, so if you ever want to change the codebase using your class to use std::span, it will be easy. I'll also use std::span as a reference and point out differences between it ...


0

std::ref to the rescue I believe that most of the places where references are used could be replaced with value type in the class and the user could just std::ref() their objects/callables (in this specific case, not in general). std::reference_wrapper provides implicit conversion to reference type (one of the main use cases include passing references to ...


Top 50 recent answers are included