{"id":104367,"date":"2020-10-14T07:00:00","date_gmt":"2020-10-14T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104367"},"modified":"2020-10-14T10:17:20","modified_gmt":"2020-10-14T17:17:20","slug":"20201014-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20201014-00\/?p=104367","title":{"rendered":"A brief introduction to C++ structured binding"},"content":{"rendered":"<p>C++17 introduced a feature known as <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/structured_binding\"> <i>structured binding<\/i><\/a>. It allows a single source object to be taken apart:<\/p>\n<pre>std::pair&lt;int, double&gt; p{ 42, 0.0 };\r\nauto [i, d] = p;\r\n\/\/ int i = 42;\r\n\/\/ double d = 0.0;\r\n<\/pre>\n<p>It seems that no two languages agree on what to call this feature. C# calls it <a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/csharp\/deconstruct\"> <i>deconstructing<\/i><\/a>. JavaScript calls it <a href=\"https:\/\/developer.mozilla.org\/docs\/Web\/JavaScript\/Reference\/Operators\/Destructuring_assignment\"> <i>destructuring<\/i><\/a>. <span style=\"text-decoration: line-through;\">(Python doesn&#8217;t seem to have a specific name for this concept, although the common case where the source is a list does have the name <i>list comprehension<\/i>.)<\/span> Python calls it <a href=\"https:\/\/docs.python.org\/3\/reference\/simple_stmts.html#assignment-statements\"> <i>unpacking<\/i><\/a>. My guess is that C++ avoided both of these terms to avoid confusion with the word <i>destructor<\/i>.<\/p>\n<p>There is a subtlety in the way structured binding works: Binding qualifiers on the <code>auto<\/code> apply to how the <i>source<\/i> is bound, not on how the <i>destination<\/i> is bound.\u00b9<\/p>\n<p>For example,<\/p>\n<pre>auto&amp;&amp; [i, d] = p;\r\n<\/pre>\n<p>becomes (approximately)\u00b9<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>If <tt>p.get&lt;N&gt;<\/tt> exists<\/th>\n<th>If <tt>p.get&lt;N&gt;<\/tt> does not exist<\/th>\n<\/tr>\n<tr>\n<td><tt><u>auto&amp;&amp;<\/u> hidden = p;<\/tt><br \/>\n<tt>decltype(auto) i = p.get&lt;0&gt;();<\/tt><br \/>\n<tt>decltype(auto) d = p.get&lt;1&gt;();<\/tt><\/td>\n<td><tt><u>auto&amp;&amp;<\/u> hidden = p;<\/tt><br \/>\n<tt>decltype(auto) i = get&lt;0&gt;(p);<\/tt><br \/>\n<tt>decltype(auto) d = get&lt;1&gt;(p);<\/tt><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>where <code>hidden<\/code> is a hidden variable introduced by the compiler. The declarations of <code>i<\/code> and <code>d<\/code> are inferred from the <code>get<\/code> method or free function.\u00b2<\/p>\n<p>(In a cruel twist of fate, if <code>hidden<\/code> is const or a const reference, then that const-ness propagates to the destinations. But the reference-ness of <code>hidden<\/code> does not propagate.)<\/p>\n<p>The <code>decltype(auto)<\/code> means that the reference-ness of the return type is preserved rather than decayed. If <code>get<\/code> returns a reference, that reference or qualifier is preserved. This differs from <code>auto<\/code> which will decay references to copies.<\/p>\n<p>All of this comes into play when you want to make your own objects available to structured binding, which we&#8217;ll look at next time.<\/p>\n<p><b>Bonus chatter<\/b>: There is no way to specify that you want only selected pieces. You must bind all the pieces.<\/p>\n<p>\u00b9 In reality, the bound variables have underlying type <code>std::tuple_element_t&lt;N, T&gt;<\/code> (where <code>N<\/code> is the zero-based index and <code>T<\/code> is the type of the source), possibly with references added. But in practice, these types match the return types of <code>get<\/code>, so it&#8217;s easier just to say that they come from <code>get<\/code>.<\/p>\n<p>\u00b2 My reading of the language specification is that the destination variables are always references:<\/p>\n<blockquote class=\"q\"><p><b>[dcl.struct.bind]<\/b><br \/>\n3. \u2026 [E]ach <tt>v<\/tt>\u1d62 is a variable of type \u201creference to <tt>T<\/tt>\u1d62\u201d initialized with the initializer, where the reference is an lvalue reference if the initializer is an lvalue and an rvalue reference otherwise.<\/p><\/blockquote>\n<p>and therefore the expansion of the structured binding would be<\/p>\n<pre><u>auto&amp;&amp;<\/u> i = get&lt;0&gt;(p);\r\n<u>auto&amp;&amp;<\/u> d = get&lt;1&gt;(p);\r\n<\/pre>\n<p>However, in practice, the compilers declare the destination variables as matching the return value of <code>get<\/code>, as I noted above.<\/p>\n<p>So I must be reading the specification wrong.<\/p>\n<p>(The text was <a href=\"https:\/\/wg21.link\/p1091r3&quot;\"> revised for C++20<\/a>, but even in the revision, it&#8217;s still a reference.)<\/p>\n<p>\u00b9 That&#8217;s because a structured binding <i>really<\/i> is a hidden variable plus a bunch of references to the pieces of that hidden variable. That&#8217;s why the qualifiers apply to the hidden variable, not to the aliases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just learning the basics.<\/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-104367","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Just learning the basics.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104367","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=104367"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104367\/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=104367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}