Preface

Another arcane text about an overly complex language! C++ is already difficult enough to master; why do people feel the need to make it even more difficult?

C++’s power comes at a price, but with the latest revisions of the language, the bar has been drastically lowered. The improvements in C++11 and C++14 have had a positive impact in many areas, from how you write a loop to how you can write templates.

We’ve had the idea of writing about template metaprogramming for a long time, because we wanted to demonstrate how much easier it has become. We also wanted to prove its usefulness and efficiency. By that we mean that it’s not only a valid solution, but sometimes the best solution.

Last but not least, even if you don’t use metaprogramming every day, understanding its concepts will make you a better programmer: you will learn to look at problems differently and increase your mastery and understanding of the language.

A Journey of a Thousand Miles Begins with a Single Step

Really mastering C++ metaprogramming is difficult and takes a lot of time. You need to understand how compilers work to get around their bugs and limitations. The feedback you can receive when you have an error is more often than not arcane.

That is the bad news.

The good news is that you don’t need to master C++ metaprogramming, because you are standing on the shoulders of giants.

In this report, we will progressively expose you to the technique and its practical applications, and give you a list of tools that you can use to get right to it.

Then, depending on your tastes and your aspirations, you can decide how deep down the rabbit hole you want to go.

Understanding Metaprogramming

Metaprogramming is a technique that can greatly increase your productivity when properly used. Improperly used, though it can result in unmaintainable code and greatly increased development time.

Dismissing metaprogramming based on a preconceived notion or dogma is counterproductive. Nevertheless, properly understanding if the technique suits your needs is paramount for fruitful and rewarding use.

An analogy we like to use is that you should see a metaprogram as a robot you program to do a job for you. After you’ve programmed the robot, it will be happy to do the task for you a thousand times, without error. Additionally, the robot is faster than you and more precise.

If you do something wrong, though, it might not be immediately obvious where the problem is. Is it a problem in how you programmed the robot? Is it a bug in the robot? Or is your program correct but the result unexpected?

That’s what makes metaprogramming more difficult: the feedback isn’t immediate, and because you added an intermediary you’ve added more variables to the equation.

That’s also why before using this technique you must ensure that you know how to program the robot.

Conventions Used in This Report

The following typographical conventions are used in this report:

Italic

Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Note

This element signifies a general note.

Warning

This element indicates a warning or caution.

Acknowledgments

This report would probably not exist without the work of Aleksey Gurtovoy and David Abrahams, authors of the Boost.MPL library and the reference book C++ Template Metaprogramming (Addison-Wesley Professional).

More recently, Eric Niebler and Peter Dimov paved the way to what modern C++ template metaprogramming should look like. They have been greatly influential in our work.

We would also like to thank all of the contributors to the Brigand library and Louis Dionne for his metaprogramming library benchmark.

Finally, we would like to thank Jon Kalb and Michael Caisse for their reviews, as well as our families, friends, and coworkers, who have been incredibly supportive.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset