{"id":103123,"date":"2019-11-22T07:00:00","date_gmt":"2019-11-22T15:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103123"},"modified":"2019-11-21T17:17:18","modified_gmt":"2019-11-22T01:17:18","slug":"20191122-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191122-00\/?p=103123","title":{"rendered":"How do I make a clone of a Windows Runtime vector in C++\/WinRT?"},"content":{"rendered":"<p>Suppose you have a Windows Runtime vector, either an <code>IVector&lt;T&gt;<\/code> or an <code>IVector\u00adView&lt;T&gt;<\/code>, and you want to clone it so that you can operate on the clone without affecting the original. For example, maybe you have an <code>IVector\u00adView&lt;T&gt;<\/code>, which is read-only, and you want to clone it so you can make changes.<\/p>\n<p>The na\u00efve way would be to copy the vector:<\/p>\n<pre>\/\/ Code in italics is wrong.\r\nIVector&lt;Thing&gt; original = GetTheThings();\r\n<i>IVector&lt;Thing&gt; clone = IVector&lt;Thing&gt;{ original };<\/i>\r\n<\/pre>\n<p>This doesn&#8217;t work because <code>IVector&lt;T&gt;<\/code> and <code>IVector\u00adView&lt;T&gt;<\/code> are interfaces, and copying an interface merely copies a reference to the same underlying object.<\/p>\n<p>To get a brand new object, you need to create a brand new object.<\/p>\n<pre>IVector&lt;Thing&gt; original = GetTheThings();\r\nstd::vector&lt;Thing&gt; temp{ original.Size() };\r\noriginal.GetMany(0, temp);\r\nIVector&lt;Thing&gt; clone = single_threaded_vector(std::move(temp));\r\n<\/pre>\n<p>First, we create a temporary vector into which we will copy the contents of the original vector.<\/p>\n<p>Next, we use the <code>Get\u00adMany<\/code> method to read the entire contents of the original <code>IVector<\/code> into our temporary vector. Note that if the vector&#8217;s size can change asynchronously, then there&#8217;s a race condition if the size changes between the time we create the temporary vector and the time we fill it with goodies. Fixing that is left as en exercise.<\/p>\n<p>Finally, we create a brand new <code>IVector<\/code> from our temporary vector.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You copy it into your own vector, but make sure you copy what you think you&#8217;re copying.<\/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-103123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You copy it into your own vector, but make sure you copy what you think you&#8217;re copying.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103123","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=103123"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103123\/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=103123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}