Automatically count the number of instantiated classes in a TMP? <p>Given a template metaprogram (TMP), do C++ compilers produce build statistics that count the number of instantiated classes? Or is there any other way to automatically get this number? So for e.g. the obiquitous factorial</p> <pre><code>#include &lt;iostream&gt; template&lt;int N&gt; struct fact { enum { value = N * fact&lt;N-1&gt;::value }; }; template&lt;&gt; struct fact&lt;1&gt; { enum { value = 1 }; }; int main() { const int x = fact&lt;3&gt;::value; std::cout &lt;&lt; x &lt;&lt; "\n"; return 0; } </code></pre> <p>I would like to get back the number 3 (since fact&lt;3>, fact&lt;2>, and fact&lt;1> are instantiated). This example if of course trivial, but whenever you start using e.g. Boost.MPL, compile times really explode, and I'd like to know how much of that is due to hidden class instantiations. My question is primarily for Visual C++, but answers for gcc would also be appreciated.</p> <p><strong>EDIT</strong>: my current very brittle approach for Visual C++ is adding the compile switch from one of Stephan T. Lavavej's <a href="http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Advanced-STL/C9-Lectures-Stephan-T-Lavavej-Advanced-STL-3-of-n">videos</a> /d1reportAllClassLayout and doing a grep + word count on the output file, but it (a) increases compile times enormously and (b) the regex is hard to get 100% correct.</p>