{"id":4362,"date":"2026-09-22T08:23:28","date_gmt":"2026-09-22T02:53:28","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=4362"},"modified":"2026-09-22T08:23:28","modified_gmt":"2026-09-22T02:53:28","slug":"c26-stdinplace_vector","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/","title":{"rendered":"C++26: std::inplace_vector"},"content":{"rendered":"<p><em><strong>C++26<\/strong><\/em> has introduced a new collection data structure called <em><strong>inplace_vector<\/strong><\/em>. It is a variant of the existing <em><strong>std::vector<\/strong><\/em> that was originally introduced in <em><strong>C++98<\/strong><\/em> with gradual improvements over the years.<\/p>\n<p>So what is <em><strong>inplace_vector<\/strong><\/em>? As the name suggests, it is a fixed capacity, in-place contiguous array. As in the case of <em><strong>vector<\/strong><\/em>, we can use methods such as <em><strong>\u201cinsert\u201d<\/strong><\/em>, <em><strong>\u201cemplace_back\u201d<\/strong><\/em>, <em><strong>\u201cpush_back\u201d<\/strong><\/em>, etc. to add elements to the collection. Here is an example to get started:<\/p>\n<figure id=\"attachment_4363\" aria-describedby=\"caption-attachment-4363\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code1.png?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"4363\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/code1-18\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code1.png\" data-orig-size=\"1400,672\" 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;,&quot;alt&quot;:&quot;&quot;}\" data-image-title=\"Adding Elements\" data-image-description=\"&lt;p&gt;Adding Elements&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Adding Elements&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code1-1024x492.png\" class=\"wp-image-4363\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code1.png?resize=600%2C288&#038;ssl=1\" alt=\"Adding Elements\" width=\"600\" height=\"288\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code1.png?resize=300%2C144&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code1.png?resize=1024%2C492&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code1.png?w=1400&amp;ssl=1 1400w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4363\" class=\"wp-caption-text\"><strong>Adding Elements<\/strong><\/figcaption><\/figure>\n<p>We create the <em><strong>inplace_vector<\/strong><\/em> to store <em><strong>\u201cstring\u201d<\/strong><\/em>, fixing its capacity as 4. This means we can add not more than 4 <em><strong>strings<\/strong><\/em> in this object. No dynamic space allocation takes place when we insert elements. Also keep in mind that the entire memory for holding the <em><strong>names<\/strong><\/em> object with all its elements is allocated on the stack. This is different from how <em><strong>vector<\/strong><\/em> operates.<\/p>\n<p>Here is the output when we run the program:<\/p>\n<figure id=\"attachment_4364\" aria-describedby=\"caption-attachment-4364\" style=\"width: 200px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output1.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"4364\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/output1-22\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output1.png\" data-orig-size=\"354,98\" 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;,&quot;alt&quot;:&quot;&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\/2026\/09\/output1.png\" class=\"wp-image-4364\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output1.png?resize=200%2C55&#038;ssl=1\" alt=\"Program Output\" width=\"200\" height=\"55\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output1.png?resize=300%2C83&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output1.png?w=354&amp;ssl=1 354w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/a><figcaption id=\"caption-attachment-4364\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>While <em><strong>\u201ccapacity\u201d<\/strong><\/em> is the upper limit on how many elements may be inserted in the object, <em><strong>\u201csize\u201d<\/strong><\/em> tells us how many actual elements live inside the object. In the above example, since we added only 3 elements, the size happens to be 3, whereas the capacity is pegged at 4.<\/p>\n<p>What happens when we try to add more elements than the <em><strong>\u201ccapacity\u201d<\/strong><\/em>? <em><strong>\u201cstd::bad_alloc\u201d<\/strong><\/em> exception is thrown.<\/p>\n<figure id=\"attachment_4365\" aria-describedby=\"caption-attachment-4365\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code4.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"4365\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/code4-5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code4.png\" data-orig-size=\"1400,1052\" 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;,&quot;alt&quot;:&quot;&quot;}\" data-image-title=\"Trying to Exceed Capacity\" data-image-description=\"&lt;p&gt;Trying to Exceed Capacity&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Trying to Exceed Capacity&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code4-1024x769.png\" class=\"wp-image-4365\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code4.png?resize=600%2C451&#038;ssl=1\" alt=\"Trying to Exceed Capacity\" width=\"600\" height=\"451\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code4.png?resize=300%2C225&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code4.png?resize=1024%2C769&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code4.png?w=1400&amp;ssl=1 1400w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4365\" class=\"wp-caption-text\"><strong>Trying to Exceed Capacity<\/strong><\/figcaption><\/figure>\n<p>Here is the program output:<\/p>\n<figure id=\"attachment_4366\" aria-describedby=\"caption-attachment-4366\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output4.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4366\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/output4-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output4.png\" data-orig-size=\"566,246\" 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;,&quot;alt&quot;:&quot;&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\/2026\/09\/output4.png\" class=\"size-medium wp-image-4366\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output4.png?resize=300%2C130&#038;ssl=1\" alt=\"Program Output\" width=\"300\" height=\"130\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output4.png?resize=300%2C130&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output4.png?w=566&amp;ssl=1 566w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-4366\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>We can also use the <em><strong>\u201ctry_push_back\u201d<\/strong><\/em> or <em><strong>\u201ctry_emplace_back\u201d<\/strong><\/em> if we decide to explicitly handle the boundary case.<\/p>\n<figure id=\"attachment_4367\" aria-describedby=\"caption-attachment-4367\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code5.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4367\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/code5-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code5.png\" data-orig-size=\"1394,1052\" 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;,&quot;alt&quot;:&quot;&quot;}\" data-image-title=\"Using &amp;#8220;try_emplace_back&amp;#8221;\" data-image-description=\"&lt;p&gt;Using &amp;#8220;try_emplace_back&amp;#8221;&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using &amp;#8220;try_emplace_back&amp;#8221;&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code5-1024x773.png\" class=\"wp-image-4367\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code5.png?resize=600%2C453&#038;ssl=1\" alt=\"Using &quot;try_emplace_back&quot;\" width=\"600\" height=\"453\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code5.png?resize=300%2C226&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code5.png?resize=1024%2C773&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code5.png?w=1394&amp;ssl=1 1394w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4367\" class=\"wp-caption-text\"><strong>Using &#8220;try_push_back&#8221;<\/strong><\/figcaption><\/figure>\n<p>The corresponding output is:<\/p>\n<figure id=\"attachment_4368\" aria-describedby=\"caption-attachment-4368\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output5.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4368\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/output5-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output5.png\" data-orig-size=\"548,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;,&quot;alt&quot;:&quot;&quot;}\" data-image-title=\"The Output\" data-image-description=\"&lt;p&gt;The Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output5.png\" class=\"size-medium wp-image-4368\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output5.png?resize=300%2C131&#038;ssl=1\" alt=\"The Output\" width=\"300\" height=\"131\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output5.png?resize=300%2C131&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output5.png?w=548&amp;ssl=1 548w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-4368\" class=\"wp-caption-text\"><strong>The Output<\/strong><\/figcaption><\/figure>\n<p>I think it is useful to touch upon a subtle point here. When we have user-defined types as elements of the <em><strong>inplace_vector<\/strong><\/em>, there is no compulsion to have a <em><strong>\u201cdefault\u201d<\/strong><\/em> no-arg constructor in that type. That is because the container elements are not automatically default initialized. The following example shows this:<\/p>\n<figure id=\"attachment_4369\" aria-describedby=\"caption-attachment-4369\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code6.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4369\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/code6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code6.png\" data-orig-size=\"1376,974\" 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;,&quot;alt&quot;:&quot;&quot;}\" data-image-title=\"No Need for Default Ctor\" data-image-description=\"&lt;p&gt;No Need for Default Ctor&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;No Need for Default Ctor&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code6-1024x725.png\" class=\"wp-image-4369\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code6.png?resize=600%2C425&#038;ssl=1\" alt=\"No Need for Default Ctor\" width=\"600\" height=\"425\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code6.png?resize=300%2C212&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code6.png?resize=1024%2C725&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/code6.png?w=1376&amp;ssl=1 1376w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4369\" class=\"wp-caption-text\"><strong>No Need for Default Constructor<\/strong><\/figcaption><\/figure>\n<p>Here is the output:<\/p>\n<figure id=\"attachment_4370\" aria-describedby=\"caption-attachment-4370\" style=\"width: 200px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output6.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4370\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2026\/09\/22\/c26-stdinplace_vector\/output6-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output6.png\" data-orig-size=\"326,74\" 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;,&quot;alt&quot;:&quot;&quot;}\" data-image-title=\"Output from Program\" data-image-description=\"&lt;p&gt;Output from Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output from Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output6.png\" class=\"wp-image-4370\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output6.png?resize=200%2C45&#038;ssl=1\" alt=\"Output from Program\" width=\"200\" height=\"45\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output6.png?resize=300%2C68&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2026\/09\/output6.png?w=326&amp;ssl=1 326w\" sizes=\"(max-width: 200px) 100vw, 200px\" \/><\/a><figcaption id=\"caption-attachment-4370\" class=\"wp-caption-text\"><strong>Output from Program<\/strong><\/figcaption><\/figure>\n<p>In the above example, there is no default constructor for <em><strong>\u201cX\u201d<\/strong><\/em> class. This won\u2019t work for <em><strong>std::array&lt;X, N&gt;<\/strong><\/em> objects!<\/p>\n<p>I hope <em><strong>std::inplace_vector<\/strong><\/em>\u00a0is clear now.<\/p>\n<p>I tested the examples in <em><strong>Compiler Explorer<\/strong><\/em> using <em><strong>\u201cx86-64 gcc 16.2\u201d<\/strong><\/em> with the compiler option <em><strong>\u201c-std=c++26\u201d<\/strong><\/em>.<\/p>\n<p>Have a great week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++26 has introduced a new collection data structure called inplace_vector. It is a variant of the existing std::vector that was originally introduced in C++98 with gradual improvements over the years. So what is inplace_vector? As the name suggests, it is a fixed capacity, in-place contiguous array. As in the case of vector, we can use [&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_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_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":[67,464,330],"class_list":["post-4362","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c","tag-stdin_place_vector","tag-stdvector"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-18m","jetpack-related-posts":[{"id":2889,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/08\/09\/stdarray-vs-stdvector\/","url_meta":{"origin":4362,"position":0},"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":4299,"url":"https:\/\/www.rangakrish.com\/index.php\/2026\/05\/14\/c23-slicing-with-stdviewschunk\/","url_meta":{"origin":4362,"position":1},"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":2876,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/07\/27\/understanding-stdspan\/","url_meta":{"origin":4362,"position":2},"title":"Understanding std::span","author":"admin","date":"July 27, 2022","format":false,"excerpt":"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\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example-1: span","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/07\/code1-285x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3184,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/08\/13\/stdis_scoped_enum\/","url_meta":{"origin":4362,"position":3},"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":[]},{"id":717,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/03\/c17-stdany\/","url_meta":{"origin":4362,"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":683,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/","url_meta":{"origin":4362,"position":5},"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":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/4362","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=4362"}],"version-history":[{"count":3,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/4362\/revisions"}],"predecessor-version":[{"id":4373,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/4362\/revisions\/4373"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=4362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=4362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=4362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}