Anyone working with generic programming or template metaprogramming in one form or another, has experienced difficulties like unexpectedly long compilation times, large executables, deeply nested compiler error messages, or type names filling up entire screens.
A recent unfortunate example is the "unexpected" instantiation of unused functions. Just for motivation, today I had another "unexpected" series of events where:
- a type, say
T
, is being tested bystd::is_empty
; - a SFINAE implementation for this (unless built-in), derives another type, say
D
, fromT
; - possibly while generating an implicit assignment operator for
D
, a (user-defined) generic assignment operator ofT
is instantiated; - because of
std::enable_if
, this operator instantiatesstd::is_assignable <M, T>
whereM
is a member or base type ofT
, and an instance of the same class template asT
; - eventually, a type trait
TR
is instantiated onT
that requiresstd::is_empty <T>
; at this point, I get an error for some type being incomplete.
I could easily patch this with some specialization of TR
, because std::is_empty
is not really needed in that particular case. "Easily" means around two hours to analyze the error messages first. What is funny is that all this was revealed when I removed a never-used, unconditional assignment operator from somewhere else (a "distant" base class of T
). The patch also speeds up compilation, but I wouldn't investigate if it wasn't for the error.
In the past I've seen compilation times drop e.g. from 2 minutes down to 10 seconds by small or larger patches, coming up just as randomly or with a little imagination.
The question is: are there systematic methods or tools to control what is happening while waiting for our template code to compile, that would help us improve the code even if it's working, similarly to how we debug programs at run time?
The only (or best) tool I've seen is Templight, that works with clang and requires a patch to the compiler. It looks cool from its Meeting C++ slides, but before trying I would appreciate any relevant ideas.
Other questions / resources I've found are for instance
- How do you debug heavily templated code in c++?
- Debugging template instantiations
- Any tool for template debugging?
but are quite outdated.