Advanced Metaprogramming in Classic C++, 3rd Edition

Advanced Metaprogramming in Classic C++, 3rd Edition

English | 2015 | ISBN: 978-1-484210-11-6 | 572 Pages | PDF | 10 MB


Advanced Metaprogramming in Classic C++: Third Edition, offers a detailed and somewhat intense look into template metaprogramming (TMP) using Classic C++ code examples. The two newer standards are not used in the code so that the examples can be rich, illustrate the point, and be run with confidence. The code can be readily adapted to include the elements of the Modern C++ standards. The gain for the reader is that TMP is presented in the book as a set of techniques that will enable a new style to your C++ coding while making it exceptionally clear and efficient.
The book deals with language aspects, design patterns, examples and applications (seen as case studies). Special emphasis is put on small reusable techniques that will improve the quality of daily work. This is a book to sit with and learn from. Users of it in prior editions point out that they come back to it over and over. This edition enhances the readability and clarity of the discussion.
The approach in the book is used to maximize compatibility and clearly illustrate the techniques, enabling the reader to comprehend difficult material without the burdens of compiler errors, and other unnecessary complexities and enabling a much more intense treatment of the subject. For those interested in Modern C++, all subsequent additions to the C++ language are fully compatible with the code in this book and users familiar with them can leverage the techniques introduced in C++XX to make the patterns in this book even more powerful. There is a chapter that discusses issues regarding the two newer standards and the basics needed to program for the newer standards are readily available online.
What makes the book exceptional is the level of understanding of the concepts involved imparted by the author. This is not just a rote overview of metaprogramming. You will truly understand difficult topics like static assertions, how to write metafunctions, overload resolution, lambda expressions, and many others. More than that, you will work through them with practical examples guided by the author’s frank explanations. This book requires you to think and to learn and to understand the language so that you can program at a higher level.
What you’ll learn
– What templates and the small object toolkit are, and how to use them
– How to do overload resolution
– How to do metaprogramming with interfaces, algorithms, functors and refactoring
– How to work with code generators
– What is opaque type principle and how to use it
– How to work with debugging templates and more
– A chapter devoted to issues surrounding C++0x and C++14
Who this book is for
This book is for experienced C++ programmers who want to learn more.

+

Templates

C++ allows programmers to express the same concept with different stylesand good C++ looks more natural. First you are going to see the connection between the templates and the style, and then you will dig into the details of the C++ template system. Given this C++ fragment:

Classic Patterns

When coding a framework/library, it’s typical to use and reuse a small set of names. For example, containers can be expected to have a member function [[integer type]] size() constthat returns the number of elements. Adopting a uniform style increases interoperability of objects; for more details, see Chapter 6.All the following paragraphs will try to describe the traditional meaning connected to a few common C++ names.

Compiler Assumptions

Heavy usage of templates implies massive work for the compiler. Not all standard-conforming techniques behave identically on every platform.

You denote by language-neutral idiomsall the language features that don’t have a standard-prescribed behavior but only a reasonable expected behavior. In other words, when you use language-neutral idiom, you can expect that most compilers will converge on some (optimal) behavior, even if they are not demanded by the standard to do so.

For example, the C++ standard prescribes that sizeof(T)>0for any type T, but does not require the size of a compound type to be minimal. an empty structcould have size 64, but we expect it to have size 1 (or at worst, a size not larger than a pointer).

A standard-conforming compiler can legally violate the optimality condition, but in practice, such a situation is rare. In other words, a language-neutral idiom is a language construction that does not make a program worse, but gives a nice opportunity of optimization to a good compiler.
Several possible problems can arise from a perfect standard-conforming code fragment:

  • Unexpected compiler errors
  • Failures at runtime (access violations, core dumps, blue screens, and panic reactions)
  • Huge compilation/link time
  • Suboptimal runtime speed

The first two issues are due to compiler bugs and involve finding a language workaround (but the second one is usually met when it’s too late).
The third problem mostly depends on poorly written template code.
The fourth problem involves finding language-neutral idioms that are not recognized by the optimizer and therefore unnecessarily slow down the program execution.
An example of expected behavior we do care about is the addition of an empty destructor to a base class

Backward Compatibility

A typical refactoring problem consists of modifying an existing routine so that any caller can choose either the original behavior or a variation.
To begin with a rather trivial example, assume you want to (optionally) log the square of each number, and you don’t want to duplicate the code. So, you can modify the classic function template sq:

Placeholders

Every C++ object can execute some actions. Empty objects, such as instance_of, can execute meta-actions, such as declare their type and “bind” their type to a template parameter or to a specific function overload. Sometimes the job of TMP is to preventwork from being done, by replacing an object with a similar empty object and an action with a corresponding meta-action. Type P<T>is called a placeholder for Tif P<T>is a class whose public interface satisfies the same pre- and post-conditions as T, but has the least possible runtime cost. In the most favorable case, it does nothing at all.

Switch-Off

The switch-off is an algorithm-refactoring technique that allows you to selectively “turn off” some features without rewriting or duplicating functions. The name comes from the paradigmatic situation where a function takes an object by reference, which is “triggered” during execution, and eventually returns an independent result, which is a by-product of the execution. The object may be a container that receives information during the execution or a synchronization object.