{"id":103904,"date":"2020-06-26T07:00:00","date_gmt":"2020-06-26T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103896"},"modified":"2020-07-02T06:49:56","modified_gmt":"2020-07-02T13:49:56","slug":"20200626-00-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200626-00\/?p=103904","title":{"rendered":"Mundane std::tuple tricks: Creating more interesting index sequences"},"content":{"rendered":"<p>Last time, <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200625-00\/?p=103890\"> we wrote <code>offset_sequence_t<\/code><\/a>, which takes an existing sequence and adds a fixed value to each element. This is the sort of index manipulation you may want to generalize. So let&#8217;s generalize it.<\/p>\n<pre>template&lt;std::size_t F(std::size_t), typename Seq&gt;\r\n    struct modify_sequence;\r\n\r\ntemplate&lt;std::size_t F(std::size_t), std::size_t... Ints&gt;\r\nstruct modify_sequence&lt;F, std::index_sequence&lt;Ints...&gt;&gt;\r\n{\r\n using type = std::index_sequence&lt;F(Ints)...&gt;;\r\n};\r\n\r\ntemplate&lt;std::size_t F(std::size_t), typename Seq&gt;\r\nusing modify_sequence_t = typename modify_sequence&lt;F, Seq&gt;::type;\r\n<\/pre>\n<p>Instead of hard-coding the <code>+ 1<\/code> operation, we let you specify the modification operation <code>F<\/code>. Since this is template metaprogramming, the modification operation must be something that can be done at compile time. This means that it needs to be a <code>constexpr<\/code> function.<\/p>\n<pre>constexpr std::size_t add_one(std::size_t v) { return v + 1; }\r\n\r\n\/\/ example = std::index_sequence&lt;4, 2, 5&gt;\r\nusing example = modify_sequence_t&lt;\r\n    add_one,\r\n    std::index_sequence&lt;3, 1, 4&gt;&gt;;\r\n<\/pre>\n<p>Of course, this gets annoying having to write <code>add_whatever<\/code> each time you want to add something, so we can generalize the <code>add_one<\/code> function.<\/p>\n<pre>template&lt;int N&gt;\r\nconstexpr std::size_t add_N(std::size_t v) { return v + N; }\r\n\r\n\/\/ example = std::index_sequence&lt;4, 2, 5&gt;\r\nusing example = modify_sequence_t&lt;\r\n    add_N&lt;1&gt;,\r\n    std::index_sequence&lt;3, 1, 4&gt;&gt;;\r\n\r\n\/\/ example2 = std::index_sequence&lt;2, 0, 3&gt;\r\nusing example2 = modify_sequence_t&lt;\r\n    add_N&lt;-1&gt;,\r\n    std::index_sequence&lt;3, 1, 4&gt;&gt;;\r\n<\/pre>\n<p>It&#8217;s possible to write other helpers like, say, <code>parallel_add_t<\/code> which adds two sequences elementwise, but that sort of thing isn&#8217;t needed in practice much.<\/p>\n<p>Next time, we&#8217;ll try to invert <code>tuple_element_t<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generalizing our manipulations so far.<\/p>\n","protected":false},"author":1069,"featured_media":100998,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103904","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Generalizing our manipulations so far.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103904","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=103904"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103904\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/100998"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}