I assume your goal is true immutability -- each object, when constructed, cannot be modified. You cannot assign one object over another.
The biggest downside to your design is that it is not compatible with move semantics, which can make functions returning such objects more practical.
As an example:
struct RockSolidLayers {
const std::vector<RockSolid> layers;
};
we can create one of these, but if we have a function to create it:
RockSolidLayers make_layers();
it must (logically) copy its contents out to the return value, or use return {}
syntax to directly construct it. Outside, you either have to do:
RockSolidLayers&& layers = make_layers();
or again (logically) copy-construct. The inability to move-construct will get in the way of a number of simple ways to have optimal code.
Now, both of those copy-constructions are elided, but the more general case holds -- you cannot move your data from one named object to another, as C++ does not have a "destroy and move" operation that both takes a variable out of scope and uses it to construct something else.
And the cases where C++ will implicitly move your object (return local_variable;
for example) prior to destruction are blocked by your const
data members.
In a language designed around immutable data, it would know it can "move" your data despite its (logical) immutability.
One way to go about this problem is to use the heap, and store your data in std::shared_ptr<const Foo>
. Now the const
ness is not in the member data, but rather in the variable. You can also only expose factory functions for each of your types that returns the above shared_ptr<const Foo>
, blocking other construction.
Such objects can be composed, with Bar
storing std::shared_ptr<const Foo>
members.
A function returning a std::shared_ptr<const X>
can efficiently move the data, and a local variable can have its state moved into another function once you are done with it without being able to mess with "real" data.
For a related technique, it is idomatic in less constrained C++ to take such shared_ptr<const X>
and store them within a wrapper type that pretends they are not immutable. When you do a mutating operation, the shared_ptr<const X>
is cloned and modified, then stored. An optimization "knows" that the shared_ptr<const X>
is "really" a shared_ptr<X>
(note: make sure factory functions return a shared_ptr<X>
cast to a shared_ptr<const X>
or this is not actually true), and when the use_count()
is 1 instead casts away const
and modifies it directly. This is an implementation of the technique known as "copy on write".
Now as C++ has developed, there are more opportunities for elision. Even C++23 is going to have more advanced elision. Elision is when the data isn't logically moved or copied, but just has two different names, one inside a function and one outside.
Relying on that remains awkward.
const
data members has an advantage in that you get an error if you forget to initialize such member of basic type. A disadvantage is that you cannot assign to variables of the type. – Illegibleconst
which is why I'd prefer to make the members private and only provideconst
getters and methods instead as oikosdev suggests – Priebe