Often, when parsing, you encounter a point where you want to check if a string is one out of a set, as quickly as possible. E.g., if you are parsing URLs, the string must start with a protocol, and there is a finite list of protocols. There are many such problems. Think about YAML files where a parameter must take one out of 3 or 5 values.
If you have 5 strings (say http:, https:, file:, sftp:, ftp:), you may do 5 comparisons. This is obviously wasteful.
In the case of my example, I am sure you can imagine a faster way to do it. To a point, compilers can do some optimization.
You can try to implement a trie: a tree-like algorithm. But that's not so fast if it is character-by-character. And it tends to be branchy.
A better approach is to use gperf, have it generate C code, and plug that into your program.
Can we do gperf at compile-time in C++?
A tool like gperf is based on heuristics. (See tech paper, in section 4, they describe their implementation. It is also open source.)
I am not sure we can implement it at compile-time in C++, but we have compile-time regular expressions in C++.
If something like this could be pulled off, then then following repo could be fully motivated.
https://github.com/ConstexprCore/useful_abstractions
Dusíková, H. (2016–2025). compile-time-regular-expressions (Version 3.10.0) [C++ library]. GitHub. https://github.com/hanickadot/compile-time-regular-expressions
Free Software Foundation. (2025). GNU gperf (Version 3.3) [Computer software]. https://www.gnu.org/software/gperf/
Schmidt, D. C., & Suda, T. (1992). GPERF: A perfect hash function generator (UC Irvine ICS Technical Report 92-47). Department of Information and Computer Science, University of California, Irvine. https://escholarship.org/uc/item/9f06z999