{"id":103804,"date":"2020-05-28T07:00:00","date_gmt":"2020-05-28T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103804"},"modified":"2020-05-28T06:19:07","modified_gmt":"2020-05-28T13:19:07","slug":"20200528-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200528-00\/?p=103804","title":{"rendered":"Template metaprogramming trick: Get the compiler to tell you what type you have"},"content":{"rendered":"<p>C++ template metaprogramming is like writing a program in Prolog without a debugger. The compiler executes your metaprogram by running through a bunch of pattern-matching rules. But unlike Prolog, the C++ template metaprogramming language doesn&#8217;t have a debugger. You just feed your code to the compiler, and you get a few possible results:<\/p>\n<ol>\n<li>It fails to compile.<\/li>\n<li>It compiles and gives you what you want.<\/li>\n<li>It compiles and gives you something that wasn&#8217;t what you want.<\/li>\n<\/ol>\n<p>Only if you&#8217;re lucky do you get case 2 on the first try.<\/p>\n<p>There&#8217;s no way to single-step through your metaprogram, and there&#8217;s no print-debugging either. All you can do is see what the compiler says.<\/p>\n<p>Here&#8217;s a trick I use to get <i>something<\/i>. It&#8217;s not great, but it&#8217;s still handy.<\/p>\n<pre>template&lt;typename... Args&gt; void whatis();\r\n<\/pre>\n<p>This is a forward declaration of a function that takes an arbitrary number of type arguments.<\/p>\n<p>I can drop a call to this function at various points in my template metaprogram to see how the compiler deduced a type:<\/p>\n<pre>template&lt;typename T&gt;\r\nvoid f(T&amp;&amp; t)\r\n{\r\n whatis&lt;T&gt;();\r\n ... other stuff ...\r\n}\r\n<\/pre>\n<p>When I instantiate <code>f<\/code>, a call to <code>whatis&lt;T&gt;<\/code> is made, among all the other stuff. I can look at the compiler output or the linker&#8217;s &#8220;unresolved external&#8221; error message to see what <code>T<\/code> ended up being.<\/p>\n<pre>double v = 3.0;\r\nf(v);\r\n\r\n\/\/ msvc\r\n\r\n??$f@AEAN@@YAXAEAN@Z PROC                   ; f&lt;double &amp;&gt;, COMDAT\r\n        ... other stuff ...\r\n        call    ??$whatis@AEAN@@YAXXZ       ; whatis&lt;<u>double &amp;<\/u>&gt;\r\n        ... other stuff ...\r\n??$f@AEAN@@YAXAEAN@Z ENDP                   ; f&lt;double &amp;&gt;\r\n\r\nunresolved external symbol\r\n\"void __cdecl whatis&lt;double &amp;&gt;()\" (??$whatis@AEAN@@YAXXZ)\r\n\r\n\/\/ gcc\r\n_Z1fIRdEvOT_:\r\n        ... other stuff ...\r\n        call    _Z6whatisIJRdEEvv\r\n        ... other stuff ...\r\n\r\nundefined reference to `void whatis&lt;double&amp;&gt;()'\r\n<\/pre>\n<p>Aha, in this instantiation of <code>f<\/code>, the type <code>T<\/code> was deduced to be <code>double&amp;<\/code>.<\/p>\n<p>It&#8217;s not a super-awesome trick, but with template metaprogramming, every little bit helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let the error message tell you.<\/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-103804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Let the error message tell you.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103804","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=103804"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103804\/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=103804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}