{"id":108408,"date":"2023-07-11T07:00:00","date_gmt":"2023-07-11T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108408"},"modified":"2023-06-19T08:33:49","modified_gmt":"2023-06-19T15:33:49","slug":"20230711-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20230711-00\/?p=108408","title":{"rendered":"Why does the compiler complain about a missing constructor when I&#8217;m just resizing my <CODE>std::vector<\/CODE> to a smaller size?"},"content":{"rendered":"<p>So you&#8217;ve got a <code>std::vector<\/code> of things.<\/p>\n<pre>std::vector&lt;Thing&gt; things;\r\n<\/pre>\n<p>And then you learn that only the first <code>n<\/code> of them are any good, so you use <code>resize()<\/code> to throw away the extras.<\/p>\n<pre>things.resize(n); \/\/ keep only the first n\r\n<\/pre>\n<p>But this doesn&#8217;t work because the compiler complains about a missing constructor for <code>Thing<\/code>. &#8220;Why is it trying to construct new <code>Thing<\/code>s when I&#8217;m just shrinking?&#8221;<\/p>\n<p>Because the compiler doesn&#8217;t know that you&#8217;re just shrinking.<\/p>\n<p>The <code>resize()<\/code> is used for both growing and shrinking, so the compiler has to generate code that is prepared to do either. And growing the vector requires default-constructing the new <code>Thing<\/code> objects.<\/p>\n<p>You might choose to appease the compiler by giving it a pre-made <code>Thing<\/code> object to use as the fill value, even though you know it won&#8217;t be used.<\/p>\n<pre>\/\/ pass dummy object to keep compiler happy\r\nthings.resize(n, dummy_thing); \/\/ keep only the first n\r\n<\/pre>\n<p>A less clunky solution is to use the <code>erase<\/code> method, which is used only for shrinking.<\/p>\n<pre>things.erase(things.begin() + n, things.end());\r\n<\/pre>\n<p>Mind you, this is still rather clunky, but at least it&#8217;s less clunky.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The compiler doesn&#8217;t know that you&#8217;re shrinking.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-108408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The compiler doesn&#8217;t know that you&#8217;re shrinking.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=108408"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108408\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=108408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}