{"id":16719,"date":"2022-11-09T05:00:00","date_gmt":"2022-11-09T04:00:00","guid":{"rendered":"https:\/\/sii.pl\/blog\/?p=16719"},"modified":"2023-02-16T15:09:46","modified_gmt":"2023-02-16T14:09:46","slug":"modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17","status":"publish","type":"post","link":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/","title":{"rendered":"Modern approach in object-oriented design with std::variant and std::visit (C++17)"},"content":{"rendered":"\n<p>Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can derive a class from another class for a hierarchy of classes that share a set of attributes and methods.<\/p>\n\n\n\n<p>It is a mechanism that allows developers to create a hierarchy between classes using an \u201eis-a\u201d relationship. The class being inherited from is called a parent class (base class) and the class inheriting is called the child class (derived class).<\/p>\n\n\n\n<p>Inheritance allows programmers to create classes built upon existing classes and specify a new implementation while maintaining the same behaviours.<\/p>\n\n\n\n<p>This article will demonstrate an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting in the same behaviour as in a well-known inheritance mechanism.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Runtime polymorphism with virtual functions<\/strong><\/h2>\n\n\n\n<p>When speaking of polymorphism immediately the first things that come to our mind are virtual functions and v-tables (C++).<\/p>\n\n\n\n<p>You declare a virtual function in a base class and then you override it in derived classes.<\/p>\n\n\n\n<p>When you call such a function on a reference or a pointer to the base class, then the compiler will invoke the correct overload. In most cases, compilers implement this technique with virtual tables (v-tables). Each class that has a virtual method contains an extra table that points to the addresses of the member functions. Before each call to a virtual method, the compiler needs to look at v-table and resolve the address of a derived function at runtime (it is called late method binding).<\/p>\n\n\n\n<p>An example below demonstrates a Vehicle base class with some interface and derived class (Pickup, Truck) which derive from it in a standard way.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Ryc.-1.png\"><img decoding=\"async\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Ryc.-1.png\" alt=\"kod\" class=\"wp-image-16720\" width=\"634\" height=\"809\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Ryc.-1.png 686w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Ryc.-1-235x300.png 235w\" sizes=\"(max-width: 634px) 100vw, 634px\" \/><\/a><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-3.png\"><img decoding=\"async\" width=\"636\" height=\"236\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-3.png\" alt=\"kod\" class=\"wp-image-16722\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-3.png 636w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-3-300x111.png 300w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/a><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-2.png\"><img decoding=\"async\" width=\"387\" height=\"356\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-2.png\" alt=\"schema\" class=\"wp-image-16724\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-2.png 387w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-2-300x276.png 300w\" sizes=\"(max-width: 387px) 100vw, 387px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Pros and Cons of run-time polymorphism with virtual functions<\/strong><\/h2>\n\n\n\n<p><strong>Pros<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>it is very convenient to extend with new class,<\/li><li>object orientation allows deep hierarchies,<\/li><li>storing heterogeneous types in a single container is easy, just store pointers to the Base class or to the abstract class (interface).<\/li><\/ul>\n\n\n\n<p><strong>Cons<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>virtual methods must be resolved before the call (performance overhead),<\/li><li>since you need a pointer to call a method, usually it means dynamic allocation (more performance overhead),<\/li><li>you need to modify the code of all classes in the inheritance structure.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Std::variant polymorphism<\/strong><\/h2>\n\n\n\n<p>The class template <strong>std::variant<\/strong>&nbsp;<strong>represents a type-safe union<\/strong>. An instance of <strong>std::variant<\/strong> at any given time either holds a value of one of its alternative types or in the case of error \u2013 no value.<\/p>\n\n\n\n<p>With this approach, there is no <strong>Vehicle<\/strong> class needed anymore as in the previous example. We can create more unrelated types here. The vehicles variable defines an object that can be a <strong>Pickup<\/strong> or <strong>Truck<\/strong>. In order to call a function we need a callable object and std::visit.<\/p>\n\n\n\n<p>A struct <strong>callPrintName<\/strong> implements overloads for the call operator. Then <strong>std::visit<\/strong> takes the variant object and calls the correct overload. std :: visit is a powerful tool that allows you to call functions on the current type stored inside std :: variant. It can find the appropriate function overloads, and what&#8217;s more, it works for multiple variants at once.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-4.png\"><img decoding=\"async\" width=\"527\" height=\"769\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-4.png\" alt=\"code\" class=\"wp-image-16726\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-4.png 527w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-4-206x300.png 206w\" sizes=\"(max-width: 527px) 100vw, 527px\" \/><\/a><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-5.png\"><img decoding=\"async\" width=\"290\" height=\"144\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-5.png\" alt=\"schema\" class=\"wp-image-16728\"\/><\/a><\/figure><\/div>\n\n\n\n<p>This can be simplified and the callable object <strong>callPrintName<\/strong> can be replaced with generic lambdas if our variant subtypes share common interface.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-6.png\"><img decoding=\"async\" width=\"544\" height=\"169\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-6.png\" alt=\"code\" class=\"wp-image-16731\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-6.png 544w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-6-300x93.png 300w\" sizes=\"(max-width: 544px) 100vw, 544px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>What if we need some arguments? The main issues is that <strong>std::visit<\/strong> does not have a way to pass arguments into the callable object. It only takes a function object and a list of <strong>std::variant<\/strong> objects.<\/p>\n\n\n\n<p>One option is to create a custom functor:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-7.png\"><img decoding=\"async\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-7.png\" alt=\"code\" class=\"wp-image-16733\" width=\"646\" height=\"501\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-7.png 826w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-7-300x233.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-7-768x596.png 768w\" sizes=\"(max-width: 646px) 100vw, 646px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>The other options to solve it by capturing the argument and forwarding it to a member function<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-8.png\"><img decoding=\"async\" width=\"604\" height=\"184\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-8.png\" alt=\"code\" class=\"wp-image-16735\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-8.png 604w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-8-300x91.png 300w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The Overload pattern<\/strong><\/h2>\n\n\n\n<p>The overload pattern is useful for wrapping multiple separate lambdas into an overload set. The code is shorter and there is no need to declare a structure that holds <strong>operator()<\/strong> overloads. In C++17 it required 2 lines of code to be implemented:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-9.png\"><img decoding=\"async\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-9.png\" alt=\"\" class=\"wp-image-16737\" width=\"756\" height=\"45\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-9.png 872w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-9-300x18.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-9-768x46.png 768w\" sizes=\"(max-width: 756px) 100vw, 756px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>The first line is the overload pattern, the second is the deduction guide for it. The struct <strong>Overloaded<\/strong><\/p>\n\n\n\n<p>can have arbitrary many base classes. It derives from each class and brings the call operator(<strong>Ts::operator..<\/strong>) of each base class into the scope. The base classes need an overloaded call operator (<strong>Ts::operator())<\/strong>. Lambdas provide this call operator. <\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-10.png\"><img decoding=\"async\" width=\"591\" height=\"237\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-10.png\" alt=\"code\" class=\"wp-image-16739\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-10.png 591w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Fig.-10-300x120.png 300w\" sizes=\"(max-width: 591px) 100vw, 591px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>The overload pattern demonstrated several C++ techniques and allows to write shorter code.&nbsp; C++17 simplifies syntax and reduces boilerplate code limiting potential errors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Pros and Cons of std::variant polymorphism<\/strong><\/h2>\n\n\n\n<p><strong>Pros<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>value semantics,<\/li><li>no dynamic allocation,<\/li><li>no need for the base class,<\/li><li>class can be unrelated,<\/li><li>duck typing: gives extra flexibility, we can call functions from visitors with a different number of arguments, return types etc. On the contrary virtual functions need to have the same signature<\/li><\/ul>\n\n\n\n<p><strong>Cons<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>need to know all types up front at compile time,<\/li><li>hard to add new types (changing type of variant and all visitors),<\/li><li>might waste memory,<\/li><li>each operation requires writing separate visitors,<\/li><li>passing parameters to <strong>std::visit<\/strong> is not as easy as with regular functions.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h2>\n\n\n\n<p>Modern C++(17) extends possibilities in terms of the runtime polymorphism approach. It leverages <strong>std::variant<\/strong> and <strong>std::visit<\/strong> to obtain that behaviour and might offer better performance and value semantics.<\/p>\n\n\n\n<p>I\u2019ve shown how you can use <strong>std::visit<\/strong> with multiple variants. Such a technique might lead to various \u201cpattern matching\u201d algorithms. You have a set of types, and you want to perform some algorithm based on the currently active types. It\u2019s like doing polymorphic operations, but differently \u2013 as <strong>std::visit<\/strong> doesn\u2019t use any v-tables.<\/p>\n\n\n\n<p>***<\/p>\n\n\n\n<p>If you want more detailed information on <strong>std::visit<\/strong> and <strong>std::variant<\/strong> and its internals please visit the link:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/mpark.github.io\/programming\/2015\/07\/07\/variant-visitation\/\" rel=\"nofollow\" >Variant visitation<\/a><\/li><li><a href=\"https:\/\/cpp-polska.pl\/post\/czym-jest-stdvariantij\" rel=\"nofollow\" >Czym jest std::variant<\/a><\/li><\/ul>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;16719&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;7&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;2&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;5\\\/5&quot;,&quot;size&quot;:&quot;30&quot;,&quot;title&quot;:&quot;Modern approach in object-oriented design with std::variant and std::visit (C++17)&quot;,&quot;width&quot;:&quot;159&quot;,&quot;_legend&quot;:&quot;{score}\\\/5&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 159px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 2px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 30px; height: 30px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 24px;\">\n            5\/5    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Inheritance is one of the core concepts of object-oriented programming (OOP) languages. It is a mechanism where you can derive &hellip; <a class=\"continued-btn\" href=\"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/\">Continued<\/a><\/p>\n","protected":false},"author":423,"featured_media":19834,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","inline_featured_image":false,"footnotes":""},"categories":[1320],"tags":[1342,1385],"class_list":["post-16719","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hard-development","tag-embedded-en","tag-cpp-en"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 4.9.9 - aioseo.com -->\n\t<meta name=\"description\" content=\"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Maciej Stok\u0142osa\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 4.9.9\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"blogersii\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Modern approach in object-oriented design with std (C++17)\" \/>\n\t\t<meta property=\"og:description\" content=\"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"733\" \/>\n\t\t<meta property=\"og:image:height\" content=\"456\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2022-11-09T04:00:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2023-02-16T14:09:46+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Sii.Polska\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Modern approach in object-oriented design with std (C++17)\" \/>\n\t\t<meta name=\"twitter:description\" content=\"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#article\",\"name\":\"Modern approach in object-oriented design with std (C++17)\",\"description\":\"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.\",\"inLanguage\":\"en-US\",\"headline\":\"Modern approach in object-oriented design with std::variant and std::visit (C++17)\",\"author\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/author\\\/mstoklosa\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/#organization\"},\"datePublished\":\"2022-11-09T04:00:00+01:00\",\"dateModified\":\"2023-02-16T14:09:46+01:00\",\"articleSection\":\"Hard development, Embedded, C++\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#webpage\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/sii.pl\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#articleImage\",\"width\":733,\"height\":456}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/hard-development\\\/#listItem\",\"name\":\"Hard development\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/hard-development\\\/#listItem\",\"position\":2,\"name\":\"Hard development\",\"item\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/hard-development\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#listItem\",\"name\":\"Modern approach in object-oriented design with std::variant and std::visit (C++17)\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#listItem\",\"position\":3,\"name\":\"Modern approach in object-oriented design with std::variant and std::visit (C++17)\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/hard-development\\\/#listItem\",\"name\":\"Hard development\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/sii.pl\\\/#organization\",\"name\":\"Sii Polska\",\"url\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/\",\"sameAs\":[\"https:\\\/\\\/www.instagram.com\\\/siipolska\\\/\",\"https:\\\/\\\/www.tiktok.com\\\/@siipolska\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCQ3UQ7RJPEYnAWC_rPE5Dxg\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/sii-polska\",\"https:\\\/\\\/clutch.co\\\/profile\\\/sii-poland\",\"https:\\\/\\\/www.crunchbase.com\\\/organization\\\/sii\",\"https:\\\/\\\/www.wikidata.org\\\/wiki\\\/Q16601096\"]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#webpage\",\"url\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/\",\"name\":\"Modern approach in object-oriented design with std (C++17)\",\"description\":\"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#breadcrumblist\"},\"author\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/author\\\/mstoklosa\\\/#author\",\"creator\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/author\\\/mstoklosa\\\/#author\",\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/sii.pl\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/11\\\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#mainImage\",\"width\":733,\"height\":456},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\\\/#mainImage\"},\"datePublished\":\"2022-11-09T04:00:00+01:00\",\"dateModified\":\"2023-02-16T14:09:46+01:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/#website\",\"url\":\"https:\\\/\\\/sii.pl\\\/blog\\\/en\\\/\",\"name\":\"blogersii\",\"description\":\"Sii\\u2019s tech blog \\u2013 practical knowledge from experts\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/sii.pl\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Modern approach in object-oriented design with std (C++17)","description":"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.","canonical_url":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#article","name":"Modern approach in object-oriented design with std (C++17)","description":"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.","inLanguage":"en-US","headline":"Modern approach in object-oriented design with std::variant and std::visit (C++17)","author":{"@id":"https:\/\/sii.pl\/blog\/en\/author\/mstoklosa\/#author"},"publisher":{"@id":"https:\/\/sii.pl\/#organization"},"datePublished":"2022-11-09T04:00:00+01:00","dateModified":"2023-02-16T14:09:46+01:00","articleSection":"Hard development, Embedded, C++","mainEntityOfPage":{"@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#webpage"},"isPartOf":{"@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#webpage"},"image":{"@type":"ImageObject","url":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg","@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#articleImage","width":733,"height":456}},{"@type":"BreadcrumbList","@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/sii.pl\/blog\/en\/#listItem","position":1,"name":"Home","item":"https:\/\/sii.pl\/blog\/en\/","nextItem":{"@type":"ListItem","@id":"https:\/\/sii.pl\/blog\/en\/hard-development\/#listItem","name":"Hard development"}},{"@type":"ListItem","@id":"https:\/\/sii.pl\/blog\/en\/hard-development\/#listItem","position":2,"name":"Hard development","item":"https:\/\/sii.pl\/blog\/en\/hard-development\/","nextItem":{"@type":"ListItem","@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#listItem","name":"Modern approach in object-oriented design with std::variant and std::visit (C++17)"},"previousItem":{"@type":"ListItem","@id":"https:\/\/sii.pl\/blog\/en\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#listItem","position":3,"name":"Modern approach in object-oriented design with std::variant and std::visit (C++17)","previousItem":{"@type":"ListItem","@id":"https:\/\/sii.pl\/blog\/en\/hard-development\/#listItem","name":"Hard development"}}]},{"@type":"Organization","@id":"https:\/\/sii.pl\/#organization","name":"Sii Polska","url":"https:\/\/sii.pl\/blog\/en\/","sameAs":["https:\/\/www.instagram.com\/siipolska\/","https:\/\/www.tiktok.com\/@siipolska","https:\/\/www.youtube.com\/channel\/UCQ3UQ7RJPEYnAWC_rPE5Dxg","https:\/\/www.linkedin.com\/company\/sii-polska","https:\/\/clutch.co\/profile\/sii-poland","https:\/\/www.crunchbase.com\/organization\/sii","https:\/\/www.wikidata.org\/wiki\/Q16601096"]},{"@type":"WebPage","@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#webpage","url":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/","name":"Modern approach in object-oriented design with std (C++17)","description":"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/sii.pl\/blog\/en\/#website"},"breadcrumb":{"@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#breadcrumblist"},"author":"https:\/\/sii.pl\/blog\/en\/author\/mstoklosa\/#author","creator":"https:\/\/sii.pl\/blog\/en\/author\/mstoklosa\/#author","image":{"@type":"ImageObject","url":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg","@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#mainImage","width":733,"height":456},"primaryImageOfPage":{"@id":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/#mainImage"},"datePublished":"2022-11-09T04:00:00+01:00","dateModified":"2023-02-16T14:09:46+01:00"},{"@type":"WebSite","@id":"https:\/\/sii.pl\/blog\/en\/#website","url":"https:\/\/sii.pl\/blog\/en\/","name":"blogersii","description":"Sii\u2019s tech blog \u2013 practical knowledge from experts","inLanguage":"en-US","publisher":{"@id":"https:\/\/sii.pl\/#organization"}}]},"og:locale":"en_US","og:site_name":"blogersii","og:type":"article","og:title":"Modern approach in object-oriented design with std (C++17)","og:description":"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.","og:url":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/","og:image":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg","og:image:secure_url":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg","og:image:width":733,"og:image:height":456,"article:published_time":"2022-11-09T04:00:00+00:00","article:modified_time":"2023-02-16T14:09:46+00:00","article:publisher":"https:\/\/www.facebook.com\/Sii.Polska","twitter:card":"summary","twitter:title":"Modern approach in object-oriented design with std (C++17)","twitter:description":"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.","twitter:image":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg"},"aioseo_meta_data":{"post_id":"16719","title":"Modern approach in object-oriented design with std (C++17)","description":"This article demonstrates an alternative approach which will allow the creation of many polymorphic implementations with a minimal amount of code resulting.","keywords":[],"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"#aioseo-article-64671fa97f64e","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"limit_modified_date":false,"created":"2022-11-03 09:24:20","updated":"2023-05-19 07:05:13","ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/sii.pl\/blog\/en\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/sii.pl\/blog\/en\/hard-development\/\" title=\"Hard development\">Hard development<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tModern approach in object-oriented design with std::variant and std::visit (C++17)\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/sii.pl\/blog\/en\/"},{"label":"Hard development","link":"https:\/\/sii.pl\/blog\/en\/hard-development\/"},{"label":"Modern approach in object-oriented design with std::variant and std::visit (C++17)","link":"https:\/\/sii.pl\/blog\/en\/modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-c17\/"}],"republish_history":[],"featured_media_url":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2022\/11\/Modern-approach-in-object-oriented-design-with-stdvariant-and-stdvisit-C17.jpg","category_names":["Hard development"],"_links":{"self":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/16719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/users\/423"}],"replies":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/comments?post=16719"}],"version-history":[{"count":4,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/16719\/revisions"}],"predecessor-version":[{"id":26768,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/16719\/revisions\/26768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media\/19834"}],"wp:attachment":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media?parent=16719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/categories?post=16719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/tags?post=16719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}