{"id":109942,"date":"2024-06-28T07:00:00","date_gmt":"2024-06-28T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109942"},"modified":"2024-06-24T08:39:10","modified_gmt":"2024-06-24T15:39:10","slug":"20240628-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240628-00\/?p=109942","title":{"rendered":"Writing a <CODE>remove_all_pointers<\/CODE> type trait, part 2"},"content":{"rendered":"<p>Last time, we <a title=\"Writing a remove_all_pointers type trait, part 1\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240627-00\/?p=109940\"> wrote a <code>remove_<wbr \/>all_<wbr \/>pointers<\/code> type trait<\/a>, but I noted that even though we found a solution, we weren&#8217;t finished yet.<\/p>\n<p>We can bring back the one-liner by using a different trick to delay the recursion: Don&#8217;t ask for the <code>type<\/code> until we know we really are recursing.<\/p>\n<p>The sketch is<\/p>\n<pre>template&lt;typename T&gt;\r\nstruct dummy { using type = T; };\r\n\r\ntemplate&lt;typename T&gt;\r\nstruct remove_all_pointers\r\n{\r\n    if (std::is_pointer_v&lt;T&gt;) {\r\n        using type_holder =\r\n            remove_all_pointers&lt;\r\n                std::remove_pointer_t&lt;T&gt;\r\n            &gt;;\r\n    } else {\r\n        using type_holder = dummy&lt;T&gt;;\r\n    }\r\n    using type =\r\n        typename type_holder::type;\r\n};\r\n<\/pre>\n<p>We first define a <code>type_holder<\/code> to be a type which has a <code>type<\/code> member type that holds our answer. If <code>T<\/code> is a pointer, then the type holder is the recursive call. Otherwise, the type holder is a dummy type whose sole purpose is to have a <code>type<\/code> member type that produces <code>T<\/code> again.<\/p>\n<p>We can now pack up that <code>if<\/code> into a <code>std::<wbr \/>conditional<\/code>.<\/p>\n<pre>template&lt;typename T&gt;\r\nstruct remove_all_pointers\r\n{\r\n    using type_holder =\r\n        std::conditional_t&lt;\r\n            std::is_pointer_v&lt;T&gt;,\r\n            remove_all_pointers&lt;\r\n                std::remove_pointer_t&lt;T&gt;\r\n            &gt;,\r\n            dummy&lt;T&gt;&gt;;\r\n    using type =\r\n        typename type_holder::type;\r\n};\r\n<\/pre>\n<p>It turns out that we don&#8217;t need to define a <code>dummy<\/code>: The C++ standard library comes with one built in! It&#8217;s called <code>std::<wbr \/>type_<wbr \/>identity&lt;T&gt;<\/code>, available starting in C++20. (<a title=\"What's the deal with std::type_identity?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240607-00\/?p=109865\">We looked at <code>std::<wbr \/>type_<wbr \/>identity&lt;T&gt;<\/code> a little while ago<\/a>.)<\/p>\n<pre>template&lt;typename T&gt;\r\nstruct remove_all_pointers\r\n{\r\n    using type_holder =\r\n        std::conditional_t&lt;\r\n            std::is_pointer_v&lt;T&gt;,\r\n            remove_all_pointers&lt;\r\n                std::remove_pointer_t&lt;T&gt;\r\n            &gt;,\r\n            std::type_identity&lt;T&gt;&gt;;\r\n    using type =\r\n        typename type_holder::type;\r\n};\r\n<\/pre>\n<p>Now we can inline the <code>type_holder<\/code>.<\/p>\n<pre>template&lt;typename T&gt;\r\nstruct remove_all_pointers\r\n{\r\n    using type =\r\n        typename std::conditional_t&lt;\r\n            std::is_pointer_v&lt;T&gt;,\r\n            remove_all_pointers&lt;\r\n                std::remove_pointer_t&lt;T&gt;\r\n            &gt;,\r\n            std::type_identity&lt;T&gt;&gt;::type;\r\n};\r\n<\/pre>\n<p>Or even better, just derive from the <code>type_holder<\/code>!<\/p>\n<pre>template&lt;typename T&gt;\r\nstruct remove_all_pointers :\r\n        std::conditional_t&lt;\r\n            std::is_pointer_v&lt;T&gt;,\r\n            remove_all_pointers&lt;\r\n                std::remove_pointer_t&lt;T&gt;\r\n            &gt;,\r\n            std::type_identity&lt;T&gt;&gt;\r\n{\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Factoring out the type resolution to after the dangerous part.<\/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-109942","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Factoring out the type resolution to after the dangerous part.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109942","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=109942"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109942\/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=109942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}