Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is an old version of boost. Click here for the latest version's documentation home page.

The Boost C++ Metaprogramming Library

Aleksey Gurtovoyi and David Abrahamsii

iMetaCommunications, agurtovoy@meta-comm.com
iiBoost Consulting, david.abrahams@rcn.com

Abstract

This paper describes the Boost C++ template metaprogramming library (MPL), an extensible compile-time framework of algorithms, sequences and metafunction classes. The library brings together important abstractions from the generic and functional programming worlds to build a powerful and easy-to-use toolset which makes template metaprogramming practical enough for the real-world environments. The MPL is heavily influenced by its run-time equivalent - the Standard Template Library (STL), a part of the C++ standard library [STL94], [ISO98]. Like the STL, it defines an open conceptual and implementation framework which can serve as a foundation for future contributions in the domain. The library's fundamental concepts and idioms enable the user to focus on solutions without navigating the universe of possible ad-hoc approaches to a given metaprogramming problem, even if no actual MPL code is used. The library also provides a compile-time lambda expression facility enabling arbitrary currying and composition of class templates, a feature whose runtime counterpart is often cited as missing from the STL. This paper explains the motivation, usage, design, and implementation of the MPL with examples of its real-life applications, and offers some lessons learned about C++ template metaprogramming.

Keywords: template metaprogramming, generic programming, programming languages, C++, STL, type systems, polymorphism, compile-time

Table of Contents

1. Introduction
1.1. Native language metaprogramming
1.2. Metaprogramming in C++
1.2.1. Numeric computations
1.2.2. Type computations
1.2.3. Type sequences
1.3. Why metaprogramming?
1.4. Why a metaprogramming library?
2. Basic usage
2.1. Conditional type selection
2.1.1. Delayed evaluation
2.2. Metafunctions
2.2.1. The simple form
2.2.2. Higher-order metafunctions
2.2.3. Metafunction classes
2.2.4. One size fits all?
2.2.5. From metafunction to metafunction class
2.3. Sequences, algorithms, and iterators
2.3.1. Introduction
2.3.2. Algorithms and sequences
2.3.3. Sequence concepts
2.3.4. Ad hoc example revisited
2.3.5. iter_fold as the main iteration algorithm
2.3.6. Sequences of numbers
2.3.7. A variety of sequences
2.3.8. Loop/recursion unrolling
3. Lambda facility
4. Code generation facilities
5. Example: a compile-time FSM generator
5.1. Implementation
5.2. Related work
6. Acknowledgements
References