{"id":104371,"date":"2020-10-16T07:00:00","date_gmt":"2020-10-16T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104371"},"modified":"2020-10-14T06:59:05","modified_gmt":"2020-10-14T13:59:05","slug":"20201016-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20201016-00\/?p=104371","title":{"rendered":"Structured binding in C++\/WinRT: The key-value pair"},"content":{"rendered":"<p>Last time, we learned <a title=\"How to add C++ structured binding support to your own types\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20201015-00\/?p=104369\"> how to add structured binding support to your own types<\/a>, and noted that the <code>get<\/code> functions do not have to return references.<\/p>\n<p>C++\/WinRT has something similar to a <code>std::pair<\/code>: The <code>IKeyValuePair&lt;K, V&gt;<\/code>, which is used to represent a single entry in a map. (C++ uses a <code>std::pair<\/code> for this.)<\/p>\n<p>Since the <code>kvp.Key()<\/code> and <code>kvp.Value()<\/code> methods always return by value, it means that when you use structured binding on an <code>IKeyValuePair<\/code>, the variables are always copies. The qualifiers on the <code>auto<\/code> merely describes how the <code>kvp<\/code> itself is captured.<\/p>\n<pre>IKeyValuePair&lt;hstring, int32_t&gt; kvp;\r\nauto&amp; [key, value] = kvp;\r\n<\/pre>\n<p>This looks like it&#8217;s binding <code>key<\/code> and <code>value<\/code> as lvalue references, but it&#8217;s not. They are non-reference variables. That&#8217;s because the code expands to<\/p>\n<pre><u>auto&amp;<\/u> hidden = kvp;\r\ndecltype(auto) key = kvp.Key();\r\ndecltype(auto) value = kvp.Value();\r\n<\/pre>\n<p>Since the results stored into <code>key<\/code> and <code>value<\/code> don&#8217;t depend on how you bound the source, you may as well bind the hidden variable by reference to avoid an unnecessary copy.<\/p>\n<pre>\/\/ wasteful copy from kvp to hidden\r\nauto [key, value] = kvp;\r\n\r\n\/\/ non-copying binding\r\nauto&amp;&amp; [key, value] = kvp;\r\n<\/pre>\n<p><b>Bonus chatter<\/b>: The structured binding of <code>IKeyValuePair<\/code> comes in particularly handy when you are iterating over something like an <code>IMap<\/code>:<\/p>\n<pre>for (auto&amp;&amp; [key, value] : map)\r\n{\r\n  \/\/ use key and value\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Taking them apart in a simpler way.<\/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-104371","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Taking them apart in a simpler way.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104371","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=104371"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104371\/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=104371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}