O'Reilly logo

Advanced Metaprogramming in Classic C++ by Davide Di Gennaro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required