# C++26's std::polymorphic lets you copy polymorphic objects without writing clone()

Original title: Composite objects and C++26 the std:polymorphic type

> Hacker News story 50148845 · language: en · submitted 2026-10-19
> Web version: https://zeli.app/story/50148845
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 46 · by [jandeboevrie](<https://news.ycombinator.com/user?id=jandeboevrie>) · 💬 33
[original](<https://mariusbancila.ro/blog/2026/10/18/composite-objects-and-cpp26-the-stdpolymorphic-type/>) · [HN discussion](<https://news.ycombinator.com/item?id=50148845>)

## Summary

Marius Bancila shows how std::polymorphic, new in C++26, restores value semantics to classes that own polymorphic components. Using an event-log example, he contrasts the old unique_ptr plus virtual clone() approach—which breaks copyability, leaks infrastructure into the interface, and fails to propagate const—with std::polymorphic, which deep-copies correctly via type erasure captured at construction, enforces deep const, and can never be null. The result: the Rule of Zero applies again.

## Key quote

> The only empty state is moved-from, explicitly detectable via valueless_after_move(), so every dereference elsewhere is valid by invariant.

## HN discussion (33 comments)

### [quietbritishjim](<https://news.ycombinator.com/item?id=50177419>)

> I followed the link near the start to the article about std::indirect<T>, which you can use instead of std::unique_ptr<T> for the pImpl idiom: [1]
>
> \> Never null: it always holds a value, except in the moved-from state (but features a member function called valueless_after_move() that lets you check the state)
>
> Ugh, what a mess. Surely the whole point of using this rather than unique_ptr is to avoid such a trap! A safe move constructor isn't possible here because you'd either have to re-init your moved-from object, or do element-wise move (with `MyClass(MyClass&& other): impl(std::in_place, *other.impl) {}`) but those would stop your move constructor being noexcept. It would've been much more useful to make the move constructor of indirect be deleted, which is what people typically did with a pImpl-based classes in the past.
>
> [1] https://mariusbancila.ro/blog/2026/07/23/the-pimpl-idiom-and...

### [usrnm](<https://news.ycombinator.com/item?id=50177136>)

> 15 years ago I would've been happy reading about all these cool new things coming to C++, nowadays I'm happy I don't use it anymore and don't need to relearn everything again. Am I getting old?

### [Panzerschrek](<https://news.ycombinator.com/item?id=50177393>)

> Does std::polymorphic require passed types to be copyable?

### [ho_schi](<https://news.ycombinator.com/item?id=50177300>)

> I need default security with a #secure and #insescure.
>
> Not adding more stuff. So I get security, legacy compatibility and the existing flexibility.

### [nananana9](<https://news.ycombinator.com/item?id=50177325>)

> We've been doing this for 50 years, you don't need a class hierarchy for your event struct.
>
> enum EventType {
>
> EVENT_NONE,
>
> EVENT_TEMPERATURE_READING,
>
> EVENT_SMOKE_DETECTED,
>
> EVENT_CARBON_MONOXIDE_DETECTED,
>
> };
>
> struct Event {
>
> EventType type;
>
> union {
>
> TemperatureDeadingData temperature_reading;
>
> SmokeDetectedData smoke_detected;
>
> CarbonMonoxideDetected carbon_monoxide_detected
>
> } data;
>
> };
>
> std::vector<Event> events;

---

## Cite

- Page: https://zeli.app/story/50148845
- Date: 2026-10-19
- Source: mariusbancila.ro (https://mariusbancila.ro/blog/2026/10/18/composite-objects-and-cpp26-the-stdpolymorphic-type/)
- Hacker News: https://news.ycombinator.com/item?id=50148845

## Other languages

- zh: https://zeli.app/zh/story/50148845 · markdown: https://zeli.app/zh/story/50148845.md
- ja: https://zeli.app/ja/story/50148845 · markdown: https://zeli.app/ja/story/50148845.md
- ko: https://zeli.app/ko/story/50148845 · markdown: https://zeli.app/ko/story/50148845.md
- de: https://zeli.app/de/story/50148845 · markdown: https://zeli.app/de/story/50148845.md
- es: https://zeli.app/es/story/50148845 · markdown: https://zeli.app/es/story/50148845.md
- ru: https://zeli.app/ru/story/50148845 · markdown: https://zeli.app/ru/story/50148845.md
