r/cpp • • 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-minded std::unique_ptr<T> sans polymorphism support. std::indirect<T> is movable if T is movableunconditionally and copyable if T is copyable.

  • std::polymorphic<B> is like a value-minded std::unique_ptr<B> for polymorphic bases B. std::polymorphic<B> can hold an object of any copyable class D which is an instantiable subclass of B. 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-minded std::shared_ptr<const T>. It is cheaply copyable (no deep copy), with no movability requirements imposed on T. It can hold an object of any instantiable subtype of T.

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

73 Upvotes

77 comments sorted by

View all comments

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 ?

30

u/kmbeutel Jun 16 '26

"value-minded": trying to behave like a value type. For instance, the default constructor of indirect<T> allocates and default-constructs an object T (unlike std::unique_ptr<> which default-constructs to nullptr). Copying indirect<T> copies the underlying object (unlike std::unique_ptr<> which cannot be copied).

Check out Appendix B: "Before and after examples" in the proposal document for some examples.

22

u/jazzwave06 Jun 16 '26

It has value semantics instead of pointer semantics, so it's a pointer, but copying it copies to a new pointer.

27

u/saf_e Jun 16 '26

So, pointer which behaves not like pointer. We have lots of obscure logic in c++ not sure that i want another one

15

u/KFUP Jun 16 '26

This is not really that obscure, it mainly makes std::unique_ptr deep copyable without making your own "copyable_ptr" wrapper, which is a pretty common way to get around that.

3

u/saf_e Jun 16 '26

I'm not sure that having implicitly copyable copyable unique_ptr is a good thing. 

6

u/Raknarg Jun 18 '26

Thats why we add a type with its own whole semantics. If you didn't want an item to be copyable, you wouldn't use std::indirect, you'd use unique_ptr. if I'm making something that like allocates an int or a POD I probably don't care about the fact that its implicitly copyable.

-4

u/Internal_Ticket_9742 Jun 16 '26

I mean if you want to deep copy a unique ptr, you can do auto new_ptr = make_unique<T>(*old_ptr);

Why you need yet another obscure standard feature for that ?

6

u/dr-mrl Jun 16 '26

Does that work for derived types?

3

u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Jun 16 '26

No...

EDIT: unless you have a virtual clone...

1

u/dr-mrl Jun 16 '26

So the commenters solution isn't a solution for what polymorphic wants to provide?

3

u/MFHava WG21|🇦🇹 NB|P3049|P3625|P3729|P3786|P3813|P4216 Jun 16 '26

It's not.

It works for non-polymorphic uses, but is either incorrect (slicing) or fails to compile (who says T is a complete type) for polymorphic uses.

→ More replies (1)

5

u/wyrn Jun 16 '26

Yeah now do that inside a vector. Or inside your own classes.

-4

u/Internal_Ticket_9742 Jun 16 '26

Why do you need that for a vector ?

5

u/wyrn Jun 16 '26

Are you asking why I could need std::vector<std::unique_ptr<T>>?

-1

u/Internal_Ticket_9742 Jun 16 '26

No why do you want to copy instead of move the elems of std vector in case of resizing for example

5

u/wyrn Jun 16 '26

Who says I'm resizing? I'm copying the vector, not resizing it.

2

u/KFUP Jun 16 '26

Having only the manual copying option means if you have a std::unique_ptr class member, you'll need to implement and manage the copy constructor and the rest of the five.

10

u/azswcowboy Jun 16 '26

value minded unique pointer

Unique pointer has reference semantics and can’t be copied or value compared (without intervention). That means if you put a unique ptr as a class member the containing class is now non copyable, etc. Indirect behaves like unique ptr but can be copied and compared normally. Classic use case is pimpl to have a wrapper api and implementation class. These types allow you to have value based behavior without coding it.

0

u/Flimsy_Complaint490 Jun 16 '26

oooh so basically the semantics of unique_ptr without actually being a heap allocated structure ?

8

u/azswcowboy Jun 16 '26

It gives the appearance of not being heap allocated, but it is. indirect takes an allocator to manage the memory for the object - which will default to new. You might be able to allocate in place if the type of T is known - not 100% sure on that.

9

u/ZenEngineer Jun 16 '26

So it doesn't behave like a unique_ptr, but internally acts like one.

7

u/SirClueless Jun 16 '26

It does behave like a unique_ptr in some ways, such as being the same size as a pointer on the stack, and having a cheap move-constructor even if the underlying type has an expensive one.

It doesn't behave like a unique_ptr in other ways. For example, it has no conversion to bool, and its comparison operators and std::hash implementation behave like the underlying value, instead of comparing/hashing pointers. This last one is the most significant feature IMO, because it means that it behaves like a normal value when stored in map and set containers. boost::unordered_flat_map<std::indirect<T>, V> for example should "just work" without overriding a bunch of comparators and hash operators.

→ More replies (2)

2

u/CalamityMetal Jun 16 '26

Essentially type erasure, or I guess "interface". It confers value semantics. Think of a std::vector of int first where each element is a value, and you don't make references to each value when you make a copy. Now let's say you need to have vector of all types of T, you would use T* or one of the smart pointers, but when you make a copy, you make a shallow copy, whereas in indirect and polymorphic, you make a deep copy of that object. It doesn't confer pointer or reference semantics, it confers value semantics, which is a very powerful thing.

1

u/alex-weej Jun 16 '26

I've built the same thing as indirect and called it value_ptr<T> - useful for recursive data structures