How to abuse a C++ compiler?

Templates were originally introduced in C++ in order to support generic programming, i.e. implementing algorithms and data structures with generic types. It was discovered by an accident that C++’s template system happens to be Turing-complete, i.e. anything theoretically computable “should” be computable on a C++ compiler during compilation.

One important corollary is that, since Turing completeness allows the definition of non-terminating computations, it is possible to write “C++ programs” whose compilation is non-terminating, which also implies that the compiler produces neither error messages nor object code. (In practice, compilers have a limit on template instantiation depth to avoid non-termination.)

In the following I am going to demonstrate a couple of code snippets containing non-terminating template computations, and how GCC (g++ 4.8.2) and Clang (clang++ 3.3) handle them.
Continue reading