Making types immutable is often desireable, especially for multi-threaded applications. There's no need to worry about concurrent access and no need for any synchronization. The common STL containers require the contained objects to be assignable, however, so they don't work with immutable types.How should I strike a balance between wanting immutable types and wanting STL-compatible types? What I could:provide an assignment operator and stop worrying about immutability. Either add synchronization or document that object assignment isn't thread-safe.live with the fact that the types can't go directly into containers and wrap them in std::unique_ptr/std::shared_ptr when required.What are the tradeoffs involved and which solution should I prefer for what types of objects? Should I strive to always make my objects STL compatible or should I only do that when I anticipate need for it? Should I strive to always make my objects immutable or only when I see concrete benefit from it?
C++: Make classes immutable or compatible with STL containers
object oriented;c++;immutability
Typical C++ is more about values that are copied than about making immutable objects that are shared. Separation instead of immutability. Try to keep the different threads' data separate.If you do want immutable shared data, you can always make a shared_ptr<T>, fill the object there with data, then hand out shared_ptr<const T> references to it. Or use something like Adobe's copy_on_write.So make your classes mutable and have them behave as proper values.