Chapter 5

Meta-Programming

“The ultimate mystery is one’s own self.”

—Sammy Davis Jr.

Meta-Programs are programs on programs. We will discuss this intriguing asset thoroughly in this chapter. In particular, we will look at three major applications of it:

  • Compile-time calculations (§5.1);

  • Information about and transformations of types (§5.2); and

  • Code generation (§5.3–§5.4).

These techniques allow us to make the examples from the preceding chapters more reliable, more efficient, and more broadly applicable. At the end of the chapter (§5.6) we will show why every algorithm can be computed during compilation in C++.

5.1 Let the Compiler Compute

Meta-programming in its full extent was probably discovered thanks to a bug. Erwin Unruh wrote in ...

Get Discovering Modern C++, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.