Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

...isn't that what templates were made for? Template metaprogramming in C++ is Turing-complete.


Not at all, originally template metaprogramming was discovered by accident.

Cannot recall any longer if the original article on the matter appeared on The C/C++ Users Journal or Dr. Dobbs.

Eventually it started to get abused and the Turing completeness has been discovered.

Since C++11, the approach to a more sane way to do metaprogramming with templates has been improving.

Instead of tag dispatch, ADL and SFINAE, we can make use of concepts, if constexpr/eval/init, type traits, and eventually reflection, instead of the old clunky ways.


“C++ Templates are Turing Complete” by Todd L. Veldhuizen has the history of discovery, early elaboration.



Templates are semi-accidentally Turing-complete. They were intended for writing compile-time-generic, run-time-concrete functions and types - but it turned out you could use them, along with the overload resolution mechanisms, to compute things. The Turing-completeness involves recursive use.

Computing things using templates is not intuitive. Many of us have gotten used to it - but that's because that's all we had for many years. It's a different sub-language within C++. As constexpr capabilities widen, we can avoid "tortuted" templates and can just write our compile-time checks and figurings in plain C++ - more or less.

Sometimes, enhanced language features in C++ allow us to actually throw away and forget about other existing features, or at least - complex and brittle idioms using existing features. Like SFINAE :-)




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: