CHAPTER 7

image

Code Generators

This chapter deals with templates that generate code—partly static, partly executed at runtime. Suppose you have to perform a simple comparison of powers:

int x = ...;if (34 < x5 < 47)

Clearly, you would like to have static constants for 34 and 47 and a corresponding runtime powering algorithm to obtain x5. However, a call to std::pow(x, 5) may be suboptimal, since 5 is a compile-time constant that might possibly be “embedded” in the call.

One of the goals of TMP is in fact to make the maximum information available to the compiler, so that it can take advantage of it.

7.1. Static Code Generators

Iteration can be used ...

Get Advanced Metaprogramming in Classic C++ 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.