{"id":103902,"date":"2020-06-24T07:00:00","date_gmt":"2020-06-24T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103886"},"modified":"2020-07-02T06:50:42","modified_gmt":"2020-07-02T13:50:42","slug":"20200624-00-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200624-00\/?p=103902","title":{"rendered":"Mundane std::tuple tricks: Selecting via an index sequence, part 2"},"content":{"rendered":"<p><a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200623-00\/?p=103883\"> Last time<\/a>, we developed the <code>select_<\/code><code>tuple<\/code> function which takes a tuple and an index sequence and produces a new tuple that selects the elements based on the index sequence. Here&#8217;s what we had:<\/p>\n<pre>\/\/ Don't use this; see discussion.\r\ntemplate&lt;typename Tuple, std::size_t... Ints&gt;\r\nauto select_tuple(Tuple&amp;&amp; tuple, std::index_sequence&lt;Ints...&gt;)\r\n{\r\n return std::make_tuple(\r\n    std::get&lt;Ints&gt;(std::forward&lt;Tuple&gt;(tuple))...);\r\n}\r\n<\/pre>\n<p>The idea is that you can do something like<\/p>\n<pre>std::tuple&lt;int, char, float&gt; t{ 1, 'x', 2.0 };\r\nauto t2 = select_tuple(t, std::index_sequence&lt;0, 2&gt;{});\r\n<\/pre>\n<p>and the result is that <code>t2<\/code> is a <code>std::tuple&lt;int, float&gt;{ 1, 2.0 }<\/code>.<\/p>\n<p>But there&#8217;s a problem with this function.<\/p>\n<p>Here&#8217;s a riddle: When does <code>std::make_tuple&lt;T&gt;()<\/code> return something that isn&#8217;t a <code>std::tuple&lt;T&gt;<\/code>?<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th><code>std::make_tuple&lt;T&gt;<\/code><\/th>\n<th>Produces <code>std::tuple&lt;T&gt;<\/code><\/th>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>int\u00a0\u00a0\u00a0<\/code><\/td>\n<td style=\"text-align: center;\" rowspan=\"4\"><code>int<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>const int\u00a0\u00a0\u00a0<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>int&amp;\u00a0\u00a0<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>int&amp;&amp;\u00a0<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>std::reference_wrapper&lt;\u00a0\u00a0\u00a0\u00a0\u00a0 int\u00a0 &gt;<\/code><\/td>\n<td style=\"text-align: center;\" rowspan=\"4\"><code>int&amp;<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>std::reference_wrapper&lt;const int\u00a0 &gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>std::reference_wrapper&lt;\u00a0\u00a0\u00a0\u00a0\u00a0 int&amp;\u00a0&gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: right;\"><code>std::reference_wrapper&lt;\u00a0\u00a0\u00a0\u00a0\u00a0 int&amp;&amp;&gt;<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Answer: When <code>T<\/code> is subject to decay or decays to a <code>reference_<\/code><code>wrapper<\/code>.<\/p>\n<p>Decay is a term in the C++ standard that refers to the changes of type that typically occur when something is passed by value to a function:<\/p>\n<ul>\n<li>References decay to the underlying type.<\/li>\n<li>cv-qualifiers (<code>const<\/code> and <code>volatile<\/code>) are removed.<\/li>\n<li>Arrays decay to pointers.<\/li>\n<li>Function decay to function pointers.<\/li>\n<\/ul>\n<p>But <code>make_<\/code><code>tuple<\/code> adds an additional wrinkle: If the decayed type is a <code>reference_<\/code><code>wrapper<\/code>, then the result is the underlying reference.<\/p>\n<p>We don&#8217;t want any of these transformations to occur. If you select a type from a tuple that is a reference, then you want the resulting tuple to have the same reference type.<\/p>\n<p>So we can&#8217;t use <code>make_<\/code><code>tuple<\/code>. We&#8217;ll specify the desired tuple type explicitly.<\/p>\n<pre>template&lt;typename Tuple, std::size_t... Ints&gt;\r\nauto select_tuple(Tuple&amp;&amp; tuple, std::index_sequence&lt;Ints...&gt;)\r\n{\r\n return std::tuple&lt;std::tuple_element_t&lt;Ints, Tuple&gt;...&gt;(\r\n    std::get&lt;Ints&gt;(std::forward&lt;Tuple&gt;(tuple))...);\r\n}\r\n<\/pre>\n<p>or alternatively<\/p>\n<pre>template&lt;typename Tuple, std::size_t... Ints&gt;\r\nstd::tuple&lt;std::tuple_element_t&lt;Ints, Tuple&gt;...&gt;\r\nselect_tuple(Tuple&amp;&amp; tuple, std::index_sequence&lt;Ints...&gt;)\r\n{\r\n return { std::get&lt;Ints&gt;(std::forward&lt;Tuple&gt;(tuple))... };\r\n}\r\n<\/pre>\n<p>Okay, now that we have this helper function, we can do a bunch of fancy tuple manipulation.<\/p>\n<p>Which we&#8217;ll do next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Preserving references.<\/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-103902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Preserving references.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103902","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=103902"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103902\/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=103902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}