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
39
u/Flimsy_Complaint490 Jun 16 '26
Im dumb - i saw the examples but I dont get it. What is application of these things ? what's a value minded std::unique_ptr ? Is it just making ownership more explicit ?
26
u/TheThiefMaster C++latest fanatic (and game dev) Jun 16 '26
That's the exact opposite of value-minded.
3
u/tavianator Jun 16 '26
Isn't it unconditionally movable? There's no need to move the underlying object, just the pointer
3
u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Jun 16 '26
As someone who really likes
indirectandpolymorphic- implemented it for our internal codebase and we've been using it in production before it was accepted into C++26 - I'm not yet convinced yourimmutableis much of an upgrade compared toshared_ptr<const T>... it pretty much only addsvalueless_after_moveas new (better) spelling foroperator bool.Relatedly, there has been a paper for
copy_on_write(P4210R0) that does something similar to yourimmutablebut adds additional value - we forwarded it to LEWG in Brno.There is definitely more we can explore in this area, IMHO we need a move-only
polymorphic... I intend to write a paper on that...5
u/azswcowboy Jun 16 '26
It might make sense, but not in the context of P3019 bc the paper is all about value semantics and hence providing the deep copy automatically. So I’d say it’s wrong to say immutable<T> is value-minded. It’s a non-null, const call only shared ptr.
10
u/PossibilityUsual6262 Jun 16 '26
What does it allow me to do easier and less error prone?
1
u/Wild_Meeting1428 Jun 17 '26
Yes it does make sense and there are already libraries doing this, e.g. immer (I am not the author):
https://github.com/arximboldi/immer/tree/master
An implementation of a similar type: https://github.com/arximboldi/immer/blob/master/immer/box.hpp
Thread safe: https://github.com/arximboldi/immer/blob/master/immer/atom.hpp
The author also compares it with shared_ptrs just with value semantics.
1
u/hoodoocat Jun 19 '26
Immutable is wrong name, because immutability is property of type contract or its implementation. It is impossible to add immutability to arbitrary type T, and by so immutable<T> is too generic and confusing name.
12
u/cristi1990an ++ Jun 16 '26
I still can't wrap around the idea that this design actually got accepted.
1
u/johannes1971 Jun 16 '26
How can it hold the value of any subclass without slicing? Is there some kind of small object optimisation going on? Or is this actually always a pointer, just one that acts a bit more value-like?
More importantly, what's the deal with it being non-nullable? I mean, it's a great property to have, if you actually really have it, and not sneak it back in through the backdoor like that! This seems like the worst of both worlds: you can't declare an empty object, but you also cannot rely on the object not being empty!
4
u/babalaban Jun 16 '26
So with those can I finally just make
std::vector<std::polymorphic<B>>that is full of derived classes D, E, G etc with value semantics and without allocating?1
u/jk_tx Jul 03 '26
I understand the point of indirect<>, but when you say your immutable<> is like a value-based shared_ptr yet has shallow copy semantics that doesn't make sense to me.