Composite objects and C++26 the std::polymorphic type
cppdashboard.dev/r/2026/10/composite-objects-and-cpp26-the-std-polymorphic-typeThis article discusses the C++ std::polymorphic type and its application in implementing composite classes. It explores various strategies for managing class hierarchies and event logging in a smart house context.
From the article
In a previous article , I discussed the C++ std::indirect type and how it helps implementing the PImpl idiom. However, the std::indirect type was inducted in the standard alongside another type called std::polymorphic . This helps implementing composite classes, where composite means a class composed of member objects in an has-a relation, where the composite own the components which are part of its values. Examples include strategy objects, AST/expression-tree nodes, shapes-style hierarchies, plugin-configured behavior – and you want the owning class to remain a regular, copyable value. In this article, we’ll look at how std::polymorphic can be used for this purpose.
Let us consider the following class hierarchy:
struct Event { virtual std::string describe() const = 0; }; struct…
Share this resource