title: Debugging template instantiations
body: <p>When doing metaprogramming using C++ templates, is there a method that can be used, sort of like a debugger, to step through how the templates are being instantiated and complied?  It seems right now, when creating a complicated network of templates, there really isn't a very good way of debugging them other than looking at the complier error messages to see how the templates are being instantiated (if there are any compiler errors), and the attempt to work backwards from the error messages if something unexpected is being generated.  I'm not really sure if what I'm looking for even exists, as it would have to be something that is done at compile time, but basically it would be a method, sort of like stepping through code and examining the stack frame in <code>gdb</code> at runtime, where the compiler could be stopped and the environment examined for the sequence by which a template or set of nested templates is being instantiated.</p>
  
  <p>For instance, let's say I created some simple code like the following:</p>
  
  <pre><code>template&lt;typename T, typename R = void&gt;
  struct int_return_type {};
  
  template&lt;typename R&gt;
  struct int_return_type&lt;int, R&gt;
  {
      typedef R type;
  };
  
  template&lt;typename T, typename R = void&gt;
  struct float_return_type {};
  
  template&lt;typename R&gt;
  struct float_return_type&lt;float, R&gt; 
  {
      typedef R type;
  };
  
  template&lt;typename T&gt;
  typename int_return_type&lt;T&gt;::type test()
  {
      cout &lt;&lt; "T type is int" &lt;&lt; endl;
  }
  
  template&lt;typename T&gt;
  typename float_return_type&lt;T&gt;::type test()
  {
      cout &lt;&lt; "T type is float" &lt;&lt; endl;
  }
  
  int main()
  {
      test&lt;int&gt;();
      test&lt;float&gt;();
      return 0;
  }
  </code></pre>
  
  <p>I know this is relatively easy code to follow, but templates can get quite a bit more involved, especially when doing metaprogramming, recursion, etc.  I understand that the complier will issue error messages that can be used to deduce how templates are being instantiated, but I'm also wondering what can be done when the actual template code is correct in a syntactic sense, but the runtime results are still incorrect.  It would be nice for instance to have a method to stop the compiler and see what <code>test</code>, as well as <code>int_return_type</code> and <code>float_return_type</code>, was being instantiated with, or what instantiations were failing.</p>
  
  <p>Are the only options available right now for debugging templates with this level of granularity 1) the compiler error messages when the code is incorrect, and 2) a combination of disassemblers and debuggers to see what instantiated code was generated if the run-time results are incorrect?  Or are there some other utilities out there that help with "watching" how templates are instantiated, and see/inspect what code is generated by the compiler to investigate and debug template errors?</p>