A recent discussion led me to the question:
Do immutable containers exist in C++? Alternately, since C++ prefers iterators to generalized containers, do immutable iterators exist?
I'm talking about something like the equivalent of Guava's ImmutableList
or the built-in Collections.unmodifiable*
methods. To be clear, those two types of collections are very different - the former is inherently immutable, while the latter is only immutable by actors who get the unmodifiable reference, but is potentially modifiable by actors who have access to the unwrapped object.
I'm interested to know if there is a standardized (actual or de-facto, as in Guava) equivalent to ImmutableCollection
:
or to ret ro = Colletions.unmodifiableCollection(orginalCollection)
(and related methods):
originalCollection
but not by anyone with the return value ro
. Usually it is implemented by wrapping the original collection in a proxy that prevents any modification, but other implementations are possible.I'm well aware of const std::iterator
and friends, but this is very different - it ensures that the a caller can pass an object to a callee, and the object won't modify it, but it does not guarantee to the callee that the object won't be modified.
Now I'm a long-time C++ user here, although admittedly not in the last 5 years, and I couldn't come up with the obvious equivalent.
There's a project called immer which might be able to handle this this need:
"immutable and persistent data structures for C++"
https://github.com/arximboldi/immer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With