{"id":2876,"date":"2022-07-27T14:32:33","date_gmt":"2022-07-27T09:02:33","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2876"},"modified":"2022-07-27T14:32:33","modified_gmt":"2022-07-27T09:02:33","slug":"understanding-stdspan","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/","title":{"rendered":"Understanding std::span"},"content":{"rendered":"<p>Introduced in <em><strong>C++20<\/strong><\/em>, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/span\" target=\"_blank\" rel=\"noopener\"><em><strong>std::span<\/strong><\/em><\/a>\u00a0is a light-weight abstraction that provides a convenient <em><strong>view<\/strong><\/em>\u00a0into a collection of <em><strong>contiguous<\/strong><\/em> elements. Note that it is not enough for the elements to be <em><strong>logically<\/strong><\/em> contiguous, but they must be contiguous in <em><strong>memory<\/strong><\/em> too. Thus, <em><strong>span<\/strong><\/em> will work with C-style arrays, C++ vectors and arrays. It will obviously not work with lists.<\/p>\n<p>The primary benefit of using <em><strong>span<\/strong><\/em> is that it provides a <em><strong>unified interface<\/strong><\/em> to work with the different types of collection.<\/p>\n<p>The following code is an example:<\/p>\n<figure id=\"attachment_2877\" aria-describedby=\"caption-attachment-2877\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2877\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/code1-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1.jpg\" data-orig-size=\"702,738\" 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;1658776899&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=\"Example-1: span\" data-image-description=\"&lt;p&gt;Example-1: span&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Example-1: span&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1.jpg\" class=\"wp-image-2877\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1.jpg?resize=500%2C526&#038;ssl=1\" alt=\"Example-1: span\" width=\"500\" height=\"526\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1.jpg?resize=285%2C300&amp;ssl=1 285w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1.jpg?w=702&amp;ssl=1 702w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2877\" class=\"wp-caption-text\"><strong>Example-1: span<\/strong><\/figcaption><\/figure>\n<p>The function <em><strong>printElements<\/strong><\/em> takes a <em><strong>span&lt;int&gt;<\/strong><\/em> as argument and prints its elements. Even though this function is called in <em><strong>main<\/strong><\/em> with three different types of collections, <em><strong>span<\/strong><\/em> provides a common interface to access the respective elements.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Here is the program output:<\/p>\n<figure id=\"attachment_2878\" aria-describedby=\"caption-attachment-2878\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2878\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/output1-10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output1.jpg\" data-orig-size=\"505,151\" 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;1658776860&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\/07\/output1.jpg\" class=\"wp-image-2878\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output1.jpg?resize=400%2C120&#038;ssl=1\" alt=\"Program Output\" width=\"400\" height=\"120\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output1.jpg?resize=300%2C90&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output1.jpg?w=505&amp;ssl=1 505w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-2878\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>The output is as expected.<\/p>\n<p>The reason <em><strong>span<\/strong><\/em> is light-weight is because it does not <em><strong>own<\/strong><\/em> the collection.<\/p>\n<p>It is also possible to work with <em><strong>dynamically allocated memory.<\/strong><\/em> The following example shows how we can use <em><strong>span<\/strong><\/em> along with <em><strong>unique_ptr<\/strong><\/em>. When constructing the <em><strong>span<\/strong><\/em> object, we additionally pass the element count.<\/p>\n<figure id=\"attachment_2879\" aria-describedby=\"caption-attachment-2879\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2879\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/code2-8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg\" data-orig-size=\"784,311\" 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;1658819101&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=\"Using span with Dynamic Allocation\" data-image-description=\"&lt;p&gt;Using span with Dynamic Allocation&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using span with Dynamic Allocation&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg\" class=\"wp-image-2879\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg?resize=550%2C218&#038;ssl=1\" alt=\"Using span with Dynamic Allocation\" width=\"550\" height=\"218\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg?resize=300%2C119&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg?resize=768%2C305&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg?resize=400%2C160&amp;ssl=1 400w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code2.jpg?w=784&amp;ssl=1 784w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2879\" class=\"wp-caption-text\"><strong>Using span with Dynamic Allocation<\/strong><\/figcaption><\/figure>\n<p><em><strong>span<\/strong><\/em> provides a convenient indexing operator <em><strong>&#8220;[]&#8221;<\/strong> <\/em>to access individual elements of the collection it references. Note that we are able to <em><strong>modify<\/strong><\/em> the original collection through the <em><strong>span<\/strong><\/em>.<\/p>\n<p>Here is the program&#8217;s output.<\/p>\n<figure id=\"attachment_2880\" aria-describedby=\"caption-attachment-2880\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2880\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/output2-11\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output2.jpg\" data-orig-size=\"569,112\" 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;1658819068&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\/07\/output2.jpg\" class=\"wp-image-2880\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output2.jpg?resize=400%2C79&#038;ssl=1\" alt=\"Program Output\" width=\"400\" height=\"79\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output2.jpg?resize=300%2C59&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output2.jpg?w=569&amp;ssl=1 569w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-2880\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>Another useful feature of <em><strong>span<\/strong><\/em> is being able to derive a <em><strong>&#8220;<\/strong><strong>subspan&#8221;<\/strong><\/em> from a given <em><strong>span<\/strong><\/em>. Here is an example:<\/p>\n<figure id=\"attachment_2881\" aria-describedby=\"caption-attachment-2881\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2881\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/code3-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code3.jpg\" data-orig-size=\"932,311\" 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;1658920166&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=\"Getting a subspan\" data-image-description=\"&lt;p&gt;Getting a subspan&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Getting a subspan&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code3.jpg\" class=\"wp-image-2881\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code3.jpg?resize=600%2C200&#038;ssl=1\" alt=\"Getting a subspan\" width=\"600\" height=\"200\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code3.jpg?resize=300%2C100&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code3.jpg?resize=768%2C256&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code3.jpg?w=932&amp;ssl=1 932w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2881\" class=\"wp-caption-text\"><strong>Getting a subspan<\/strong><\/figcaption><\/figure>\n<p>To further confirm that the <em><strong>subspan<\/strong><\/em> points to the second element (index: 1) of the original array, we compare the addresses. Look at the program&#8217;s output:<\/p>\n<figure id=\"attachment_2882\" aria-describedby=\"caption-attachment-2882\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2882\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/output3-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output3.jpg\" data-orig-size=\"551,159\" 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;1658920226&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=\"Checking subspan\" data-image-description=\"&lt;p&gt;Checking subspan&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Checking subspan&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output3.jpg\" class=\"wp-image-2882\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output3.jpg?resize=400%2C115&#038;ssl=1\" alt=\"Checking subspan\" width=\"400\" height=\"115\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output3.jpg?resize=300%2C87&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/output3.jpg?w=551&amp;ssl=1 551w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-2882\" class=\"wp-caption-text\"><strong>Checking subspan<\/strong><\/figcaption><\/figure>\n<p>The output shows that the values match, and even the addresses agree.<span class=\"Apple-converted-space\">\u00a0We can therefore conclude that the <em><strong>subspan<\/strong><\/em> is still a view into the original collection.<\/span><\/p>\n<p><span class=\"Apple-converted-space\">Thus, <em><strong>span<\/strong><\/em> can be convenient when working with contiguously stored collections.<\/span><\/p>\n<p>That concludes our discussion of <em><strong>std::span<\/strong><\/em>.<\/p>\n<p>Have a nice week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduced in C++20, std::span\u00a0is a light-weight abstraction that provides a convenient view\u00a0into a collection of contiguous elements. Note that it is not enough for the elements to be logically contiguous, but they must be contiguous in memory too. Thus, span will work with C-style arrays, C++ vectors and arrays. It will obviously not work with [&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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[49,17],"tags":[328,327],"class_list":["post-2876","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c-collection","tag-stdspan"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Ko","jetpack-related-posts":[{"id":2024,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/","url_meta":{"origin":2876,"position":0},"title":"std::is_standard_layout","author":"admin","date":"June 21, 2020","format":false,"excerpt":"In our last post, we learnt about the type trait std::is_trivial<T>. Today, let us go through another type trait that is quite similar. The expression\u00a0is_standard_layout<T>::value returns true if the layout of objects of type T is compiler independent, and hence is of standard format. Else, it returns false. This is\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"The Difference","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":717,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/","url_meta":{"origin":2876,"position":1},"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":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":2876,"position":2},"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":2889,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/","url_meta":{"origin":2876,"position":3},"title":"std::array vs. std::vector","author":"admin","date":"August 9, 2022","format":false,"excerpt":"One of the common questions when starting to study the container abstractions in C++20 is \"Should I use std::array\u00a0or std::vector?\". The correct answer is \"use what is best suited for the current situation\". That is not good enough, obviously. In this short article, I will try to answer this question\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"std::array Example","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/08\/code1-300x150.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":2876,"position":4},"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":2876,"position":5},"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":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2876","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=2876"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2876\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}