@TECHREPORT{Veldhuizen03c++templates, author = {Todd L. Veldhuizen}, title = {C++ Templates are Turing Complete}, institution = {}, year = {2003} }

Bookmark

OpenURL

Abstract

We sketch a proof of a well-known folk theorem that C++ templates are Turing complete. The absence of a formal semantics for C++ template instantiation makes a rigorous proof unlikely.

...we sketch a proof that C++ templates are Turing complete. We assume familiarity with both C++ templates and basic theory of computation; for background on Turing machines readers are referred to e.g. =-=[2]-=-. The proof is straightforward: we show how any Turing machine may be embedded in the C++ template instantiation mechanism, from which the result is immediate. 2 Encoding Turing machines in C++ Templa...

...es a rigorous proof unlikely. 1 Introduction It has been known for some time that C++ templates permit complicated computations to be performed at compile time. Thesrst example was due to Erwin Unruh =-=[3]-=- who circulated a small C++ program that computed prime numbers at compile time, and listed them encoded as compiler error messages. In this short note we sketch a proof that C++ templates are Turing ...

...ble. In recognition of this diculty, the C++ standards committee allows conforming compilers to limit the depth of \recursively nested template instantiations," with a recommended minimum limit o=-=f 17 [1]-=-. Compilers have adopted this limit, many with an option to increase it. template struct K17 f static const int x = K17 >::x + K17 >::x + K17 >::x + K17 >::x + K17 >::x; g; template struct K17 f stati...