{"id":717,"date":"2017-12-03T08:31:23","date_gmt":"2017-12-03T08:31:23","guid":{"rendered":"http:\/\/www.rangakrish.com\/?p=717"},"modified":"2017-12-03T09:52:30","modified_gmt":"2017-12-03T09:52:30","slug":"c17-stdany","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/","title":{"rendered":"C++17: std::any"},"content":{"rendered":"<p>In the previous two posts, I talked about <b><i>std::variant&lt;&gt;<\/i><\/b> and <b><i>std::optional&lt;&gt;<\/i><\/b>. Today, I want to take up <b><i>std::any <\/i><\/b>for discussion.<\/p>\n<p>The type <b><i>any <\/i><\/b>(implemented by the class <b><i>any<\/i><\/b>) allows a variable to hold a single value of any type. More interestingly, the type of the value held by a variable of type <b><i>any <\/i><\/b>can even change dynamically. In this sense, it is different from <b><i>variant&lt;&gt; <\/i><\/b>and <b><i>optional&lt;&gt;<\/i><\/b>.<\/p>\n<p>We have to include the header <b><i>&lt;any&gt; <\/i><\/b>in order to use this type<i>.<\/i><\/p>\n<p>Let us start with a simple example and explore the various features of this type. Consider the following code snippet.<\/p>\n<figure id=\"attachment_718\" aria-describedby=\"caption-attachment-718\" style=\"width: 578px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"718\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example1-6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png\" data-orig-size=\"578,240\" 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\/12\/Example1.png\" class=\"size-full wp-image-718\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=578%2C240\" alt=\"Example1\" width=\"578\" height=\"240\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?w=578&amp;ssl=1 578w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1.png?resize=300%2C125&amp;ssl=1 300w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/a><figcaption id=\"caption-attachment-718\" class=\"wp-caption-text\"><strong>Example1<\/strong><\/figcaption><\/figure>\n<p>Here we create two variables of <b><i>any <\/i><\/b>type, one initialized with integer value and the other with a string. To retrieve the contained value of the variable, we need to use the templated <b><i>any_cast&lt;&gt;() <\/i><\/b>function. This function can also be used to assign a new value to the variable, but of the same type as the existing value.<\/p>\n<p>It is OK to define a variable of <b><i>any <\/i><\/b>type without initializing it; the value could be set later. We can use the member function <em><strong>has_value()<\/strong><\/em> to check if the variable has a value. See the following example.<\/p>\n<figure id=\"attachment_719\" aria-describedby=\"caption-attachment-719\" style=\"width: 531px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"719\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example2-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2.png\" data-orig-size=\"531,272\" 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\/12\/Example2.png\" class=\"size-full wp-image-719\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2.png?resize=531%2C272\" alt=\"Example2\" width=\"531\" height=\"272\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2.png?w=531&amp;ssl=1 531w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example2.png?resize=300%2C154&amp;ssl=1 300w\" sizes=\"(max-width: 531px) 100vw, 531px\" \/><\/a><figcaption id=\"caption-attachment-719\" class=\"wp-caption-text\"><strong>Example2<\/strong><\/figcaption><\/figure>\n<p>Notice how we can directly assign a value to the variable. We do not have to use <em><strong>any_cast&lt;T&gt;(v)<\/strong> <\/em>as we did in the earlier example.<\/p>\n<p>The next example given below shows how we can assign different value types to the same\u00a0<b><i>any <\/i><\/b>type variable.<\/p>\n<figure id=\"attachment_720\" aria-describedby=\"caption-attachment-720\" style=\"width: 437px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3.png\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"720\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example3-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3.png\" data-orig-size=\"437,199\" 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\/12\/Example3.png\" class=\"size-full wp-image-720\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3.png?resize=437%2C199\" alt=\"Example3\" width=\"437\" height=\"199\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3.png?w=437&amp;ssl=1 437w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example3.png?resize=300%2C137&amp;ssl=1 300w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><\/a><figcaption id=\"caption-attachment-720\" class=\"wp-caption-text\"><strong>Example3<\/strong><\/figcaption><\/figure>\n<p>It is important to remember that when we use <b><i>any_cast&lt;T&gt;(v)<\/i><\/b>, we have to use the current value type.<\/p>\n<p>What happens if we use <b><i>any_cast&lt;T&gt;(v) <\/i><\/b>to access a variable, but the type we specify is different from the current value type? This causes an exception of type <b><i>bad_any_cast <\/i><\/b>to be thrown. See this code fragment:<\/p>\n<figure id=\"attachment_721\" aria-describedby=\"caption-attachment-721\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"721\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example4-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4.png\" data-orig-size=\"640,176\" 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\/12\/Example4.png\" class=\"size-full wp-image-721\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4.png?resize=640%2C176\" alt=\"Example4\" width=\"640\" height=\"176\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4.png?w=640&amp;ssl=1 640w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example4.png?resize=300%2C83&amp;ssl=1 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-721\" class=\"wp-caption-text\"><strong>Example4<\/strong><\/figcaption><\/figure>\n<p>It is possible to destroy the value contained in an <b><i>any <\/i><\/b>type variable using the <em><strong>reset()<\/strong><\/em> member function. Once reset, the variable holds no value. This is illustrated below.<\/p>\n<figure id=\"attachment_722\" aria-describedby=\"caption-attachment-722\" style=\"width: 554px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example5.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"722\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example5-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example5.png\" data-orig-size=\"554,281\" 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\/12\/Example5.png\" class=\"size-full wp-image-722\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example5.png?resize=554%2C281\" alt=\"Example5\" width=\"554\" height=\"281\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example5.png?w=554&amp;ssl=1 554w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example5.png?resize=300%2C152&amp;ssl=1 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/a><figcaption id=\"caption-attachment-722\" class=\"wp-caption-text\"><strong>Example5<\/strong><\/figcaption><\/figure>\n<p>So far, we have shown the <b><i>any <\/i><\/b>type variable being used with primitive value types such as integer. Can the type hold values of user-defined types? Yes, of course. Here is how:<\/p>\n<figure id=\"attachment_723\" aria-describedby=\"caption-attachment-723\" style=\"width: 493px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example6.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"723\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example6-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example6.png\" data-orig-size=\"493,210\" 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\/12\/example6.png\" class=\"size-full wp-image-723\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example6.png?resize=493%2C210\" alt=\"Example6\" width=\"493\" height=\"210\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example6.png?w=493&amp;ssl=1 493w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example6.png?resize=300%2C128&amp;ssl=1 300w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><\/a><figcaption id=\"caption-attachment-723\" class=\"wp-caption-text\"><strong>Example6<\/strong><\/figcaption><\/figure>\n<p>There is one requirement when we use user-defined types with <b><i>any<\/i><\/b>. And that is, the user-defined type must be <b><i>copy constructible<\/i><\/b>. In other words, it must have a <em><strong>copy constructor<\/strong><\/em>. In the example below, <b><i>struct Y <\/i><\/b>has a <b><i>deleted <\/i><\/b>copy constructor, and hence it cannot be used with <b><i>any <\/i><\/b>type.<\/p>\n<figure id=\"attachment_724\" aria-describedby=\"caption-attachment-724\" style=\"width: 518px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example7.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"724\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example7-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example7.png\" data-orig-size=\"518,178\" 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\/12\/example7.png\" class=\"size-full wp-image-724\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example7.png?resize=518%2C178\" alt=\"Example7\" width=\"518\" height=\"178\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example7.png?w=518&amp;ssl=1 518w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example7.png?resize=300%2C103&amp;ssl=1 300w\" sizes=\"(max-width: 518px) 100vw, 518px\" \/><\/a><figcaption id=\"caption-attachment-724\" class=\"wp-caption-text\"><strong>Example7<\/strong><\/figcaption><\/figure>\n<p>Here is another cool thing. Normally collections such as arrays and vectors require that the contained elements belong to the same type. Of course, we can use inheritance and pointers to store objects belonging to the same hierarchy. But when you create an array or vector of <em><strong>any<\/strong><\/em> type, you can actually store objects of unrelated types! This is illustrated in the following example.<\/p>\n<figure id=\"attachment_725\" aria-describedby=\"caption-attachment-725\" style=\"width: 646px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example8.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"725\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example8.png\" data-orig-size=\"646,116\" 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=\"Example8\" data-image-description=\"&lt;p&gt;Example8&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example8&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example8.png\" class=\"size-full wp-image-725\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example8.png?resize=646%2C116\" alt=\"Example8\" width=\"646\" height=\"116\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example8.png?w=646&amp;ssl=1 646w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example8.png?resize=300%2C54&amp;ssl=1 300w\" sizes=\"(max-width: 646px) 100vw, 646px\" \/><\/a><figcaption id=\"caption-attachment-725\" class=\"wp-caption-text\"><strong>Example8<\/strong><\/figcaption><\/figure>\n<p>Notice how we can use the <b><i>type() <\/i><\/b>member function of <b><i>any <\/i><\/b>to get the type description of the contained object.<\/p>\n<p>As the last point to discuss, let us see if there is any space overhead in using <b><i>any <\/i><\/b>type. The following code fragment uses the <b><i>sizeof()<\/i><\/b> operator to print the number of bytes allocated to many objects of <b><i>any <\/i><\/b>type, each holding a different value type.<\/p>\n<figure id=\"attachment_726\" aria-describedby=\"caption-attachment-726\" style=\"width: 587px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example9.png\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"726\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/example9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example9.png\" data-orig-size=\"587,237\" 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=\"Example9\" data-image-description=\"&lt;p&gt;Example9&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example9&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example9.png\" class=\"size-full wp-image-726\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example9.png?resize=587%2C237\" alt=\"Example9\" width=\"587\" height=\"237\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example9.png?w=587&amp;ssl=1 587w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example9.png?resize=300%2C121&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/example9.png?resize=400%2C160&amp;ssl=1 400w\" sizes=\"(max-width: 587px) 100vw, 587px\" \/><\/a><figcaption id=\"caption-attachment-726\" class=\"wp-caption-text\"><strong>Example9<\/strong><\/figcaption><\/figure>\n<p>What is surprising is that each of the variables, irrespective of the respective value type, seems to have been allocated 40 bytes! Even if the variable is uninitialized, this is indeed the case.<\/p>\n<p>Given the flexibility that <b><i>any <\/i><\/b>gives us in storing any value type, and even changing it dynamically, we are prepared for additional overhead, but why this much (constant value)? I am certain this is implementation dependent.<\/p>\n<p>That is <em><strong>std::any<\/strong><\/em> for you. Hope you found the discussion interesting. The example source is available\u00a0<a href=\"http:\/\/www.rangakrish.com\/downloads\/Any.cpp\" target=\"_blank\" rel=\"noopener\">here<\/a>. I tested this on VS 2017.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous two posts, I talked about std::variant&lt;&gt; and std::optional&lt;&gt;. 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 of type any can even [&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_post_was_ever_published":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}},"categories":[49,17],"tags":[118,124],"class_list":["post-717","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c17","tag-stdany"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-bz","jetpack-related-posts":[{"id":700,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/21\/c17-stdoptional\/","url_meta":{"origin":717,"position":0},"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":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":717,"position":1},"title":"C++17: std::variant","author":"admin","date":"November 5, 2017","format":false,"excerpt":"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 <variant>\u00a0to use this feature. Let us look at a simple example to get started: First, we define a variant object v1\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.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/11\/Example1.png?resize=525%2C300 1.5x"},"classes":[]},{"id":3106,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/","url_meta":{"origin":717,"position":2},"title":"std::expected","author":"admin","date":"May 13, 2023","format":false,"excerpt":"I had written about std::optional<> in an earlier article. C++23 introduces std::expected<> as an interesting extension to std::optional<>. Whereas std::optional<> contains a value or none at all, std::expected<> contains a value or an error code associated with it. This gives better control to the caller in terms of handling the\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"An Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/05\/source1-300x265.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":717,"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":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":717,"position":4},"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":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":717,"position":5},"title":"std::is_scoped_enum","author":"admin","date":"August 13, 2023","format":false,"excerpt":"The type trait \"std::is_scoped_enum<T>::value\" was introduced in C++23 to check whether the type \u201cT\u201d is a scoped enum type. Another way to use this is std::is_scoped_enum_v<T>. Before getting into this trait in detail, let us briefly recap the differences between unscoped and scoped enums. Unscoped Enums Unscoped enums are the\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Unscoped Enums","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/08\/unscoped-300x110.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/717","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=717"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/717\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}