Appendix A. An Introduction to Preprocessor Metaprogramming

A.1 Motivation

Even with the full power of template metaprogramming and the Boost Metaprogramming Library at our disposal, some C++ coding jobs still require a great deal of boilerplate code repetition. We saw one example in Chapter 5, when we implemented tiny_size:

   template <class T0, class T1, class T2>   struct tiny_size     : mpl::int_<3> {};

Aside from the repeated pattern in the parameter list of the primary template above, there are three partial specializations below, which also follow a predictable pattern:

   template <class T0, class T1>   struct tiny_size<T0,T1,none>     : mpl::int_<2> {};   template <class T0>   struct tiny_size<T0,none,none>     : mpl::int_<1> {}; ...

Get C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond 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.