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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.