r/cpp • u/kmbeutel • Jun 16 '26
immutable<>, complement of C++26 std::indirect<> and std::polymorphic<>
C++26 introduces std::indirect<> and std::polymorphic<> (reference implementation at github.com/jbcoe/value_types):
std::indirect<T>is like a value-mindedstd::unique_ptr<T>sans polymorphism support.std::indirect<T>is movableifunconditionally and copyable ifTis movableTis copyable.std::polymorphic<B>is like a value-mindedstd::unique_ptr<B>for polymorphic basesB.std::polymorphic<B>can hold an object of any copyable classDwhich is an instantiable subclass ofB.std::polymorphic<B>is copyable; its copy constructor will polymorphically clone the underlying object.
Both types are designed to be non-nullable. For lack of destructive move semantics, both have a moved-from state which can be identified with the valueless_after_move() member function.
As far as I can tell, the design of these is based on Sean Parent's "concept–model idiom". Remembering his presentation on the topic (https://sean-parent.stlab.cc/papers-and-presentations/#value-semantics-and-concept-based-polymorphism), I noticed that there is an obvious complement to indirect<> and polymorphic<> which I provisionally dub immutable<>:
immutable<T>is like a value-mindedstd::shared_ptr<const T>. It is cheaply copyable (no deep copy), with no movability requirements imposed onT. It can hold an object of any instantiable subtype ofT.
Possible implementation + some tests on Compiler Explorer
Does this make sense? I find it very useful for building persistent data structures. In fact, it seems so obvious to me that I'm surprised this wasn't already in P3019.
Edit: minor correction
Edit 2: another minor correction, thanks /u/tavianator
View all comments
Show parent comment
0
u/kmbeutel Jun 17 '26 edited Jun 17 '26
Thank you for this informative reply.
My first draft of
immutable<>was simply calledshared<>, but that felt a bit too technical -- the shared state is an implementation detail.immutable<>makes it clear that the value is safe to access concurrently. That cannot be assumed forshared_ptr<const T>because someone else might hold a non-const reference to the object (as others have pointed out).copy_on_write<T>sounds like a good alternative, except that the version proposed by P4210R0 doesn't support polymorphic types:I understand why a separate class would be required; copy-on-write needs copyability, and for polymorphic objects, copying needs to be polymorphic as well, which would be unnecessary overhead in the non-polymorphic case.
immutable<>doesn't have this problem; it doesn't need copies and supports both polymorphic and non-polymorphic use cases. And I'd argue thatimmutable<>is not much harder to use thancopy_on_write<>for non-polymorphic modifications. You could implementmodify()as a free function:This way, copyability is required only when modifications are needed.
(
modify()is probably still best implemented as a member function so the object can be copy-constructed in-place and the extra move can be avoided.)(One of the reasons why I thought
immutable<>was obvious was that Sean Parent's presentation on "Inheritance is the base class of Evil" demonstrated the concept–model idiom with a polymorphic example built uponshared_ptr<const Shape>. Sean does not seem to be involved with P4210 so far, but I still find it puzzling that this use case has not been covered by any of the proposals yet.)