Say that I have a C++ class, Container
, that contains some elements of type Element
. For various reasons, it is inefficient, undesirable, unnecessary,
The STL doesn't define any lesser concepts; mostly because the idea of const
is usually expressed on a per-iterator or per-reference level, not on a per-class level.
You shouldn't provide iterator
with unexpected semantics, only provide const_iterator
. This allows client code to fail in the most logical place (with the most readable error message) if they make a mistake.
Possibly the easiest way to do it would be to encapsulate it and prevent all non-const aliases.
class example {
std::list stuff;
public:
void Process(...) { ... }
const std::list& Results() { return stuff; }
};
Now any client code knows exactly what they can do with the return value of Results- nada that requires mutation.