title: Finding non-instantiated templates in C++ code
body: <p>What is the best way to find uninstantiated templates in C++ Code?</p>
  
  <p>I have a code base that heavily uses templates.
  Of course, we want to make sure that the test coverage is high.
  For all used code, this works very well using <code>gcov</code>.</p>
  
  <p>However, unused templates are reported as non-executable by <code>gcov</code>.</p>
  
  <p>After some googling, it appears that there is no way to force <code>g++</code> to emit code for these templates (which is only logical, how should the compiler guess any types?)
  It also appears that there is no way to make gcov recognize the uninstantiated template code as runnable code.</p>
  
  <p>Is there anything "out of the box" that allows me to augment the files generated by the GCC <code>-ftest-coverage -fprofile-arcs</code> instrumentation? By the <a href="http://gcc.gnu.org/onlinedocs/gcc/Gcov-Data-Files.html#Gcov-Data-Files" rel="noreferrer">gcov option documentation of GCC</a>, it would probably be enough to mark the whole template function body as one block since execution will never end up there.</p>
  
  <p><strong>EDIT</strong> (background information): I'm working on a header-only template library.
  My aim here is to find unused/untested functions.</p>
  
  <p>I know that code coverage is flawed but finding uninstantiated code is a very important step towards better tested code.
  Currently, we are putting checkpoint macros at the beginning of each function.
  In testing mode, they expand to code that inserts a pair of <code>(file, line)</code> into a global set of passed checkpoints.
  After running the tests, we manually read all files and compare the reached checkpoints with the set of all available checkpoints.</p>
  
  <p>Finding uninstantiated code is important, for example, because of unintuitive C++ template precedence behaviour, there might be dead code somewhere that the reader or even the author would expect to be used.</p>