7

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 by std::is_empty;
  • a SFINAE implementation for this (unless built-in), derives another type, say D, from T;
  • possibly while generating an implicit assignment operator for D, a (user-defined) generic assignment operator of T is instantiated;
  • because of std::enable_if, this operator instantiates std::is_assignable <M, T> where M is a member or base type of T, and an instance of the same class template as T;
  • eventually, a type trait TR is instantiated on T that requires std::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

but are quite outdated.

Community
  • 1
  • 1
iavr
  • 7,547
  • 1
  • 18
  • 53
  • Putting the question on hold: I understand this may be a generic or difficult question. That's why I hadn't asked so far; I only tried because of today's "events". Unfortunately, I cannot think of how to improve or make it more specific. – iavr Mar 31 '14 at 15:31

0 Answers0