{"id":683,"date":"2017-11-05T13:54:42","date_gmt":"2017-11-05T13:54:42","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=683"},"modified":"2017-11-05T14:00:59","modified_gmt":"2017-11-05T14:00:59","slug":"c17-stdvariant","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","title":{"rendered":"C++17: std::variant<>"},"content":{"rendered":"<p>C++17 introduces a new type-safe union in the form of <em><strong>std::variant<\/strong><\/em>. At any time, it can store a single value from one of many types. We need to <em><strong>include &lt;variant&gt;<\/strong><\/em>\u00a0to use this feature.<\/p>\n<p>Let us look at a simple example to get started:<\/p>\n<figure id=\"attachment_684\" aria-describedby=\"caption-attachment-684\" style=\"width: 570px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"684\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/example1-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png\" data-orig-size=\"570,450\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example1\" data-image-description=\"&lt;p&gt;Example1&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example1&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png\" class=\"size-full wp-image-684\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=570%2C450\" alt=\"Example1\" width=\"570\" height=\"450\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?w=570&amp;ssl=1 570w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=300%2C237&amp;ssl=1 300w\" sizes=\"(max-width: 570px) 100vw, 570px\" \/><\/a><figcaption id=\"caption-attachment-684\" class=\"wp-caption-text\"><strong>Example1<\/strong><\/figcaption><\/figure>\n<p>First, we define a variant object <em><strong>v1<\/strong><\/em> to hold either an <em><strong>int<\/strong><\/em> or <em><strong>string<\/strong><\/em> type, and initialize it with an integer value. The alternative elements can each be accessed by an index value, starting from 0. The left-most alternative gets zero, the next one gets 1, and so on. In this example, <em><strong>int<\/strong><\/em> will corrspond to index 0 and <em><strong>string<\/strong><\/em> will get index 1. The <em><strong>index()<\/strong><\/em> member function returns the index for the valid alternative.<\/p>\n<p>We use the <em><strong>std::get&lt;&gt;()<\/strong><\/em> function to reference the currently valid alternative. Since the current value is integer, we use <em><strong>get&lt;int&gt;(v1)<\/strong><\/em>. We could also use <em><strong>get&lt;0&gt;(v1)<\/strong><\/em>.<\/p>\n<p>We can assign a value directly to the variant object, or through the active index. Note that trying to access the invalid alternative using the wrong index will\u00a0 throw a run-time exception of type <em><strong>std::bad_variant_access<\/strong><\/em>. On the other hand, it is a compile-time error to use an index outside the valid range.<\/p>\n<p>The correct variant alternative will be selected automatically based on the initialization expression. If we initialize with a string literal, the <em><strong>string<\/strong><\/em> alternative is selected. And in this case, the valid active index will be 1, not zero.<\/p>\n<p>See the next example:<\/p>\n<figure id=\"attachment_685\" aria-describedby=\"caption-attachment-685\" style=\"width: 507px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"685\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/example2-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2.png\" data-orig-size=\"507,177\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example2\" data-image-description=\"&lt;p&gt;Example2&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example2&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2.png\" class=\"size-full wp-image-685\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2.png?resize=507%2C177\" alt=\"Example2\" width=\"507\" height=\"177\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2.png?w=507&amp;ssl=1 507w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example2.png?resize=300%2C105&amp;ssl=1 300w\" sizes=\"(max-width: 507px) 100vw, 507px\" \/><\/a><figcaption id=\"caption-attachment-685\" class=\"wp-caption-text\"><strong>Example2<\/strong><\/figcaption><\/figure>\n<p>What if we do not initialize at all? In this case, the left-most alternative must allow default initialization, else it is a compile-time error. In the above example, the <em><strong>int<\/strong><\/em> element will be initialized with 0, and the <em><strong>index()<\/strong><\/em> function will return 0.<\/p>\n<p>Let us try using some user-defined type. Consider the <em><strong>struct X<\/strong><\/em> given below:<\/p>\n<figure id=\"attachment_686\" aria-describedby=\"caption-attachment-686\" style=\"width: 569px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"686\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/struct-x\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x.png\" data-orig-size=\"569,123\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"User Type\" data-image-description=\"&lt;p&gt;User Type&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;User Type&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x.png\" class=\"size-full wp-image-686\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x.png?resize=569%2C123\" alt=\"User Type\" width=\"569\" height=\"123\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x.png?w=569&amp;ssl=1 569w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/struct-x.png?resize=300%2C65&amp;ssl=1 300w\" sizes=\"(max-width: 569px) 100vw, 569px\" \/><\/a><figcaption id=\"caption-attachment-686\" class=\"wp-caption-text\"><strong>User Type<\/strong><\/figcaption><\/figure>\n<p>Note that <em><strong>X<\/strong><\/em> does not have a default constructor. Let us consider using <em><strong>X<\/strong><\/em> as a type alternative in <em><strong>std::variant<\/strong><\/em>.<\/p>\n<figure id=\"attachment_687\" aria-describedby=\"caption-attachment-687\" style=\"width: 651px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"687\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/example3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3.png\" data-orig-size=\"767,278\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example3\" data-image-description=\"&lt;p&gt;Example3&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example3&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3.png\" class=\"wp-image-687\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3.png?resize=651%2C236\" alt=\"Example3\" width=\"651\" height=\"236\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3.png?w=767&amp;ssl=1 767w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example3.png?resize=300%2C109&amp;ssl=1 300w\" sizes=\"(max-width: 651px) 100vw, 651px\" \/><\/a><figcaption id=\"caption-attachment-687\" class=\"wp-caption-text\"><strong>Example3<\/strong><\/figcaption><\/figure>\n<p>The first definition (commented out) does not compile because we are depending on default initialization and <em><strong>X<\/strong><\/em>, being the left-most type, does not have a default constructor.<\/p>\n<p>When we change the order of types, it works because <em><strong>int<\/strong><\/em> will become the active type with a default value of 0.<\/p>\n<p>In the next definition, we supply a proper initialization value for <em><strong>X<\/strong><\/em>. This results in a call to <em><strong>X<\/strong><\/em> constructor to instantiate a new object and <em><strong>X<\/strong><\/em> becomes the active type.<\/p>\n<p>The next statement is interesting. We assign an integer value to the variable. Since the previous value was of type <em><strong>X<\/strong><\/em>, the compiler cannot just overwrite that area with the integer value, but it must destroy <em><strong>X<\/strong><\/em> first! This is indeed what happens, and this is the most important difference between a union and a variant. <em><strong>std::variant<\/strong> <\/em>thus guarantees type safety!<\/p>\n<p>We saw earlier that if we pass an incorrect index (or type) to <em><strong>std::get<\/strong><\/em>, we get <em><strong>std::bad_variant_access<\/strong><\/em> exception. Alternatively, we can\u00a0 use <em><strong>std::get_if<\/strong><\/em>. In this case we have to pass a pointter to the variant object. If the index (or type) matches the active element, then a pointer to the value will be returned. If not, a <em><strong>nullptr<\/strong><\/em> will be returned. This is illustrated in the following example:<\/p>\n<figure id=\"attachment_688\" aria-describedby=\"caption-attachment-688\" style=\"width: 709px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"688\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/example4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4.png\" data-orig-size=\"709,379\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example4\" data-image-description=\"&lt;p&gt;Example4&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example4&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4.png\" class=\"size-full wp-image-688\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4.png?resize=709%2C379\" alt=\"Example4\" width=\"709\" height=\"379\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4.png?w=709&amp;ssl=1 709w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example4.png?resize=300%2C160&amp;ssl=1 300w\" sizes=\"(max-width: 709px) 100vw, 709px\" \/><\/a><figcaption id=\"caption-attachment-688\" class=\"wp-caption-text\"><strong>Example4<\/strong><\/figcaption><\/figure>\n<p>Alternatively, we can use the function <em><strong>std::holds_alternative()<\/strong><\/em> to check if the variant currently holds a value of given type. See the following example:<\/p>\n<figure id=\"attachment_689\" aria-describedby=\"caption-attachment-689\" style=\"width: 580px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"689\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/example5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5.png\" data-orig-size=\"580,165\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example5\" data-image-description=\"&lt;p&gt;Example5&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example5&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5.png\" class=\"size-full wp-image-689\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5.png?resize=580%2C165\" alt=\"Example5\" width=\"580\" height=\"165\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5.png?w=580&amp;ssl=1 580w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example5.png?resize=300%2C85&amp;ssl=1 300w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/a><figcaption id=\"caption-attachment-689\" class=\"wp-caption-text\"><strong>Example5<\/strong><\/figcaption><\/figure>\n<p>The next example shows how we can use <em><strong>variant::emplace()<\/strong><\/em> member function to construct an object in-place.<\/p>\n<figure id=\"attachment_690\" aria-describedby=\"caption-attachment-690\" style=\"width: 506px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example6.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"690\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/example6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example6.png\" data-orig-size=\"506,110\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example6\" data-image-description=\"&lt;p&gt;Example6&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example6&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example6.png\" class=\"size-full wp-image-690\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example6.png?resize=506%2C110\" alt=\"Example6\" width=\"506\" height=\"110\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example6.png?w=506&amp;ssl=1 506w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example6.png?resize=300%2C65&amp;ssl=1 300w\" sizes=\"(max-width: 506px) 100vw, 506px\" \/><\/a><figcaption id=\"caption-attachment-690\" class=\"wp-caption-text\"><strong>Example6<\/strong><\/figcaption><\/figure>\n<p>The library guarantees that the previous object will be destroyed before constructing the new object in its place.<\/p>\n<p>The last funcationality we will discuss is <em><strong>std::visit<\/strong><\/em>. Let us suppose that we have a collection of variant objects, and we want to visit each element of the collection and do something on it. How can we do this? There are a few interesting ways, but to keep the discussion brief, we will look at a basic approach.<\/p>\n<p>Let us first define a function object that has overloaded versions for the different variant alternatives (we use member template for this). We can pass the function object to the <em><strong>std::visit<\/strong><\/em> function along with the variant. This causes the appropriate function (based on the active variant alternative) to be applied on the variant element. This is shown in the following figure.<\/p>\n<figure id=\"attachment_691\" aria-describedby=\"caption-attachment-691\" style=\"width: 520px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example7.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"691\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/example7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example7.png\" data-orig-size=\"520,239\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Example7\" data-image-description=\"&lt;p&gt;Example7&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example7&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example7.png\" class=\"size-full wp-image-691\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example7.png?resize=520%2C239\" alt=\"Example7\" width=\"520\" height=\"239\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example7.png?w=520&amp;ssl=1 520w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example7.png?resize=300%2C138&amp;ssl=1 300w\" sizes=\"(max-width: 520px) 100vw, 520px\" \/><\/a><figcaption id=\"caption-attachment-691\" class=\"wp-caption-text\"><strong>Example7<\/strong><\/figcaption><\/figure>\n<p>Thus<em><strong> std::variant<\/strong><\/em> is a useful concept when you are looking for a type-safe discriminated union. Check it out.<\/p>\n<p>I used Visual Studio Profession 2017, Version 15.4.0 for this article. The source code can be downloaded from\u00a0<a href=\"http:\/\/www.rangakrish.com\/downloads\/Variant Example.cpp\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++17 introduces a new type-safe union in the form of std::variant. At any time, it can store a single value from one of many types. We need to include &lt;variant&gt;\u00a0to use this feature. Let us look at a simple example to get started: First, we define a variant object v1 to hold either an int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[49,17],"tags":[118,121],"class_list":["post-683","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c17","tag-stdvariant"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-b1","jetpack-related-posts":[{"id":717,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/","url_meta":{"origin":683,"position":0},"title":"C++17: std::any","author":"admin","date":"December 3, 2017","format":false,"excerpt":"In the previous two posts, I talked about std::variant<> and std::optional<>. Today, I want to take up std::any for discussion. The type any (implemented by the class any) allows a variable to hold a single value of any type. More interestingly, the type of the value held by a variable\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":700,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/","url_meta":{"origin":683,"position":1},"title":"C++17: std::optional","author":"admin","date":"November 21, 2017","format":false,"excerpt":"Suppose we want to write a function that returns a value, but with the possibility that\u00a0 the computation might fail. This failure can be represented as an exception, or as a return value that unamibiguously denotes failure (for example, -1). Throwing an exception is a strong form of failure and\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1-3.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":683,"position":2},"title":"std::is_destructible","author":"admin","date":"March 15, 2020","format":false,"excerpt":"In the last article, I explained the deleted destructor in some detail. Today, I would like to talk about a related construct, a type trait called std::is_destructible. Type traits, defined in the header <type_traits>, are a big help when it comes to implementing template metaprogramming. \u00a0See this\u00a0article for a nice\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Private\/Protected Destructor","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example7.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1973,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/26\/stdis_base_of\/","url_meta":{"origin":683,"position":3},"title":"std::is_base_of","author":"admin","date":"April 26, 2020","format":false,"excerpt":"In our on-going series on C++ Type Traits, today's topic is about the trait std::is_base_of<>. For the official description, see this. std::is_base_of<A, B>::value takes two arguments, both classes (or structs), and returns true if A is a base class of B and false otherwise. Trivially, std::<A, A>::value is true. Let\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Output.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1960,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/04\/12\/stdcommon_type-type-trait\/","url_meta":{"origin":683,"position":4},"title":"std::common_type Type Trait","author":"admin","date":"April 12, 2020","format":false,"excerpt":"In today's post, I would like to go over the type trait std::common_type<>. This trait was introduced in C++11. As per the specification, std::common_type<T1, T2, ...Tn>::type refers to a type Tx in the given list, which the rest of the types in the list can be implicitly converted to. This\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Program Output","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/04\/Fig5.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":198,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/04\/08\/c14-return-type-deduction-for-normal-functions\/","url_meta":{"origin":683,"position":5},"title":"C++14: Return Type Deduction for Normal Functions","author":"admin","date":"April 8, 2016","format":false,"excerpt":"In C++11, a function must specify its return type in one of the following two ways: int aFunction(int arg) { \u00a0 \u00a0 return arg * 2; } (or) auto aFunction(int arg) -> int { \u00a0 \u00a0 return arg * 2; } The first is the classic function definition syntax. The\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=683"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/683\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}