Polymorphism with std::variant Visitors
Polymorphism with std::variant<> Visitors • no pointers • no inheritance in appl. code • objects located together • no common base class • no virtual functions Draw Circle Line class Circle { private: Coord center; int r; public: Circle (Coord c, int r); void move(Coord c); void draw() const; }; class Line { private: Coord from; Coord to; public: Line (Coord f, Coord t); void move(Coord c); void draw() const; }; using Geometry = std::variant<Circle, Line>; std::vector<Geometry> fig; fig.push_back(Line(Coord(1, 2), Coord(3, 4))); fig.push_back(Circle(Coord(5, 6), 7)); return 0; void drawLines(const std::vector<Geometry>& v) { for (const auto& geom : v) { std::visit([](const auto& obj) { obj.draw(); // polymorphic call }, geom); } } visitor uses a local vtable std::vector<Geometry> fig = createFig(); drawLines(fig); // remove all elements in the vector fig.clear(); 96 C++17 Josuttis.com
A presentation slide explains polymorphism in C++17 using `std::variant` and visitors. It contrasts this approach with traditional inheritance, highlighting no pointers, no common base class, no virtual functions, and a local vtable. The joke is aimed at programmers who recognize the complexity and potential traps of advanced C++ design, so it is shared as a technical reaction or ironic programming meme. EN: A presentation slide explains polymorphism in C++17 using `std::variant` and visitors. It contrasts this approach with traditional inheritance, highlighting no pointers, no common base class, no virtual functions, and a local vtable. The joke is aimed at programmers who recognize the complexity and potential traps of advanced C++ design, so it is shared as a technical reaction or ironic programming meme.
- @ithueti / 828 2019-04-19
- Type
- image
- Size
- 800×600 · 46.8 KiB
- Mood
- ироничный, технический
- Language
- en
- Added