{"id":103810,"date":"2020-05-29T07:00:00","date_gmt":"2020-05-29T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103810"},"modified":"2020-06-01T07:57:30","modified_gmt":"2020-06-01T14:57:30","slug":"20200529-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200529-00\/?p=103810","title":{"rendered":"Hiding C++ template parameter packs in a tuple"},"content":{"rendered":"<p>C++11 introduced variadic templates and template parameter packs.<\/p>\n<pre>template&lt;typename... Args&gt;\r\nstruct S;\r\n<\/pre>\n<p>Passing template parameter packs around is a bit of a hassle, because the dots &#8220;soak up&#8221; parameters, which make them hard to pass to other templated types. You also can&#8217;t &#8220;save&#8221; parameter packs in another type:<\/p>\n<pre>template&lt;typename... Args&gt;\r\nstruct S\r\n{\r\n    \/\/ doesn't compile: cannot create a type that\r\n    \/\/ is itself a parameter pack\r\n    using TheArgs = Args...;\r\n};\r\n<\/pre>\n<p>One workaround for this is to capture the types into a <code>std::tuple<\/code>.<\/p>\n<pre>template&lt;typename... Args&gt;\r\nstruct S\r\n{\r\n    using Tuple = std::tuple&lt;Args...&gt;;\r\n};\r\n<\/pre>\n<p>You can then pass the <code>Tuple<\/code> around, and if you need to extract the types, you can pull them out of the tuple.<\/p>\n<p>My first thought about how to extract the types was to use <code>std::get<\/code>:<\/p>\n<pre>\/\/ code in italics is wrong\r\ntemplate&lt;typename... Args&gt;\r\nstruct Traits\r\n{\r\n    using Tuple = std::tuple&lt;Args...&gt;;\r\n    static constexpr auto Size = sizeof...(Args);\r\n    template &lt;std::size_t N&gt;\r\n    <i>using Nth = decltype(std::get&lt;N&gt;(std::declval&lt;Tuple&gt;()));<\/i>\r\n    using First = Nth&lt;0&gt;;\r\n    using Last  = Nth&lt;Size - 1&gt;;\r\n};\r\n<\/pre>\n<p>This doesn&#8217;t work because <code>std::get<\/code> returns a reference:<\/p>\n<pre>void f()\r\n{\r\n whatis&lt;Traits&lt;int, double&gt;::First&gt;();\r\n}\r\n\r\n    call    void whatis&lt;int&amp;&amp;&gt;()\r\n<\/pre>\n<p>This behavior is presumably so you can modify the tuple:<\/p>\n<pre>std::tuple&lt;int&gt; tuple;\r\nstd::get&lt;0&gt;(tuple) = 2;\r\n<\/pre>\n<p>Fortunately, there&#8217;s another way to extract the type from a tuple: <code>std::tuple_element<\/code>.<\/p>\n<pre>template&lt;typename... Args&gt;\r\nstruct Traits\r\n{\r\n    using Tuple = std::tuple&lt;Args...&gt;;\r\n    static constexpr auto Size = sizeof...(Args);\r\n    template &lt;std::size_t N&gt;\r\n    <span style=\"color: blue;\">using Nth = typename std::tuple_element&lt;N, Tuple&gt;::type;<\/span>\r\n    using First = Nth&lt;0&gt;;\r\n    using Last  = Nth&lt;Size - 1&gt;;\r\n};\r\n<\/pre>\n<p>This provides a simpler way to extract the last type from a template parameter pack than writing some horrible recursive template metaprogram, which is what some people did instead of hiding the pack inside a tuple.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So you can pull them back out.<\/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-103810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>So you can pull them back out.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103810","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=103810"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103810\/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=103810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}