{"id":2889,"date":"2022-08-09T14:31:33","date_gmt":"2022-08-09T09:01:33","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2889"},"modified":"2022-08-11T05:19:09","modified_gmt":"2022-08-10T23:49:09","slug":"stdarray-vs-stdvector","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/","title":{"rendered":"std::array vs. std::vector"},"content":{"rendered":"<p>One of the common questions when starting to study the <em><strong>container<\/strong><\/em> abstractions in <em><strong>C++20<\/strong><\/em> is <em><strong>&#8220;Should I use <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/array\" target=\"_blank\" rel=\"noopener\">std::array<\/a>\u00a0or <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/vector\" target=\"_blank\" rel=\"noopener\">std::vector<\/a>?&#8221;<\/strong><\/em>. The correct answer is <em><strong>&#8220;use what is best suited for the current situation&#8221;<\/strong><\/em>. That is not good enough, obviously. In this short article, I will try to answer this question based on the intrinsic nature of these two containers.<\/p>\n<p><em><strong>std::array<\/strong><\/em> is a convenient <em><strong>wrapper<\/strong><\/em> over C-style native array. It is therefore no less efficient than using the native array. I would recommend this nice <a href=\"https:\/\/www.learncpp.com\/cpp-tutorial\/an-introduction-to-stdarray\/\" target=\"_blank\" rel=\"noopener\"><em><strong>article<\/strong><\/em><\/a>\u00a0if you wish to learn more about this container.<\/p>\n<p>This also means that when you create an object of <em><strong>std::array<\/strong><\/em> as a local variable inside a function, for example, the whole space is reserved on that function&#8217;s stack, just like the C-style array. In other words, the storage for the elements is not allocated separately on the heap, and it cannot grow or shrink! Here is a short code fragment to illustrate the point:<\/p>\n<figure id=\"attachment_2890\" aria-describedby=\"caption-attachment-2890\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2890\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/code1-6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1.jpg\" data-orig-size=\"924,461\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1659957387&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=\"std::array Example\" data-image-description=\"&lt;p&gt;std::array Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;std::array Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1.jpg\" class=\"wp-image-2890\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1.jpg?resize=600%2C299&#038;ssl=1\" alt=\"std::array Example\" width=\"600\" height=\"299\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1.jpg?resize=300%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1.jpg?resize=768%2C383&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1.jpg?w=924&amp;ssl=1 924w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2890\" class=\"wp-caption-text\"><strong>std::array Example<\/strong><\/figcaption><\/figure>\n<p>When you run the program, this is the output:<\/p>\n<figure id=\"attachment_2891\" aria-describedby=\"caption-attachment-2891\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2891\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/output1-11\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output1.jpg\" data-orig-size=\"572,101\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1659958729&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=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output1.jpg\" class=\"wp-image-2891\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output1.jpg?resize=450%2C79&#038;ssl=1\" alt=\"Program Output\" width=\"450\" height=\"79\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output1.jpg?resize=300%2C53&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output1.jpg?w=572&amp;ssl=1 572w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-2891\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>The above example clearly tells us that the space occupied by an <em><strong>std::array<\/strong><\/em> object is the same as a native array and is reserved statically. This also gives us a warning signal: when we use <em><strong>std::array<\/strong><\/em> as a local variable, we have to be careful about its size (i.e, number of elements in the array). Depending on the memory constraints on the machine\/device where our code will run, we have to ensure that there is sufficient stack space for creating the local object. It is difficult to recommend a specific threshold, but if you need space for a <em><strong>large<\/strong><\/em> array, the stack is probably not the right area for it.<\/p>\n<p>Now, what about <em><strong>std::vector<\/strong><\/em>? Unlike <em><strong>std::array<\/strong><\/em>, <em><strong>std::vector<\/strong><\/em> does not allocate all the required memory statically (it cannot know a priori). Although the implementation might reserve some memory initially, it is meant to grow dynamically. Hence the memory for the elements it contains is allocated on the heap (of course, the vector object will contain some data for book keeping purposes and this will be of fixed size).<\/p>\n<p>Take a look at the following code:<\/p>\n<figure id=\"attachment_2892\" aria-describedby=\"caption-attachment-2892\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2892\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/code2-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg\" data-orig-size=\"897,358\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1659968327&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=\"std::vector Example\" data-image-description=\"&lt;p&gt;std::vector Example&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;std::vector Example&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg\" class=\"wp-image-2892\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg?resize=600%2C239&#038;ssl=1\" alt=\"std::vector Example\" width=\"600\" height=\"239\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg?resize=300%2C120&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg?resize=768%2C307&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg?resize=400%2C160&amp;ssl=1 400w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code2.jpg?w=897&amp;ssl=1 897w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2892\" class=\"wp-caption-text\"><strong>std::vector Example<\/strong><\/figcaption><\/figure>\n<p>Note that the variable <em><strong>\u201cvector1\u201d<\/strong><\/em> is created empty and no items are added later. The other vectors have different number of items. Let us see what happens when we run this program:<\/p>\n<figure id=\"attachment_2893\" aria-describedby=\"caption-attachment-2893\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2893\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/output2-12\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output2.jpg\" data-orig-size=\"574,109\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1659958676&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=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output2.jpg\" class=\"wp-image-2893\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output2.jpg?resize=450%2C85&#038;ssl=1\" alt=\"Program Output\" width=\"450\" height=\"85\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output2.jpg?resize=300%2C57&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/output2.jpg?w=574&amp;ssl=1 574w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-2893\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>You can see that irrespective of the number of items the vector contains, the size of the vector object is a constant value of 32 bytes (implementation dependent). This size corresponds to the state variables (pointer, etc.) that are part of the object and does not reflect the items added to the vector.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>The above two examples give us clues as to the usage pattern of these two containers. When we do not know, a priori, the number of items our container will hold, <em><strong>std::vector<\/strong><\/em> is the one to consider. When the number of items is known ahead of time and is not a large number, <em><strong>std::array<\/strong><\/em> can be considered.<\/p>\n<p>What to do if we know the number of items ahead of time, but the number is large? We can still use <em><strong>std::array<\/strong><\/em>, but create it on heap instead, perhaps using <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/memory\/unique_ptr\" target=\"_blank\" rel=\"noopener\"><em><strong>std::unique_ptr<\/strong><\/em><\/a>. The following code shows how:<\/p>\n<figure id=\"attachment_2894\" aria-describedby=\"caption-attachment-2894\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2894\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/code3-6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code3.jpg\" data-orig-size=\"858,257\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1660053887&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=\"Creating std::array on the Heap\" data-image-description=\"&lt;p&gt;Creating std::array on the Heap&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Creating std::array on the Heap&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code3.jpg\" class=\"wp-image-2894\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code3.jpg?resize=550%2C165&#038;ssl=1\" alt=\"Creating std::array on the Heap\" width=\"550\" height=\"165\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code3.jpg?resize=300%2C90&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code3.jpg?resize=768%2C230&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code3.jpg?w=858&amp;ssl=1 858w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2894\" class=\"wp-caption-text\"><strong>Creating std::array on the Heap<\/strong><\/figcaption><\/figure>\n<p><em><strong>std::array<\/strong><\/em> and <em><strong>std::vector<\/strong><\/em> are among the most commonly used containers, so it is important to become comfortable with their proper usage.<\/p>\n<p>The example source is available <a href=\"http:\/\/www.rangakrish.com\/downloads\/std_array.cpp\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>Have a nice week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the common questions when starting to study the container abstractions in C++20 is &#8220;Should I use std::array\u00a0or std::vector?&#8221;. The correct answer is &#8220;use what is best suited for the current situation&#8221;. That is not good enough, obviously. In this short article, I will try to answer this question based on the intrinsic nature [&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":true,"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":[329,331,330],"class_list":["post-2889","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-stdarray","tag-stdunique_ptr","tag-stdvector"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-KB","jetpack-related-posts":[{"id":1928,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/15\/stdis_destructible\/","url_meta":{"origin":2889,"position":0},"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":1091,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/10\/14\/c17-stdapply-and-stdinvoke\/","url_meta":{"origin":2889,"position":1},"title":"C++17 &#8211; std::apply() and std::invoke()","author":"admin","date":"October 14, 2018","format":false,"excerpt":"Calling a function (or function object) dynamically, through a pointer known at runtime, is a common programming scenario. Almost all languages support this use case. Lisp, for example, has apply and funcall. When using apply, you can see that the arguments are passed via a separate list object. With funcall,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Calling Member Functions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/10\/Code5.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2975,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/12\/25\/stdtie\/","url_meta":{"origin":2889,"position":2},"title":"Std::tie","author":"admin","date":"December 25, 2022","format":false,"excerpt":"std::tuple is a widely used abstraction in C++ and has been around since C++11. It is a generalization of std::pair. std::tie is convenient when we want to create a tuple of lvalue references to existing variables. It is a function template commonly used to unpack a tuple into individual objects.\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::tie Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/12\/Example1-300x142.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":4299,"url":"https:\/\/www.rangakrish.com\/index.php\/2026\/05\/14\/c23-slicing-with-stdviewschunk\/","url_meta":{"origin":2889,"position":3},"title":"C++23: Slicing with std::views::chunk","author":"admin","date":"May 14, 2026","format":false,"excerpt":"One of the common use cases in programming involves extracting fixed size chunks from a given sequence and operating on them. Not a big deal at all. As a C++ developer, I have coded this quite often and have wished that the language had a built-in function for this. Fortunately,\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Using the chunk function","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/05\/example1-300x259.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/05\/example1-300x259.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/05\/example1-300x259.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":717,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/","url_meta":{"origin":2889,"position":4},"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":3106,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/05\/13\/stdexpected\/","url_meta":{"origin":2889,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2889","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=2889"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2889\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}