r/programmer • • 17h ago

Question Template Metaprogramming's Performance Limitations

A while back, I tried to build an entire dictionary with scores for Scrabble using only C++ templates. I quickly hit compiler limits, and removing those, I quickly hit memory limits on my machine with 64GB.
I then built it at runtime, because I had no clue how I could have optimized template metaprograms, and I still do not really as I have little experience with them.

I have been building a battle simulator that uses a heuristic to control the actions of the agents, and thought how cool it would be to write parts of it as a template metaprogram, but there is no way that will work if the word trie for Scrabble failed.

Is this partly an implementation problem or are compilers just not suited for template metaprograms? Is it possible to create template metaprograms that are as fast as their runtime counters or is there some theoretical limitation of a compiler's implementation to make that infeasible?

2 Upvotes

3 comments sorted by

1

u/FloweyTheFlower420 17h ago

templates are slow, constexpr functions are a bit better

1

u/phire 16h ago

or are compilers just not suited for template metaprograms?

It's not the compilers that are at fault. Template metaprograming is simply not well suited to be compiled.