logoalt Hacker News

chrisrodrigue04/23/20252 repliesview on HN

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


Replies

pjmlp04/23/2025

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.

show 1 reply
einpoklum04/23/2025

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 :-)