Item.55: Familiarize yourself with Boost.

Item.55: Familiarize yourself with Boost.

Searching for a collection of high-quality, open source, platform- and compiler-independent libraries? Look to Boost. Interested in joining a community of ambitious, talented C++ developers working on state-of-the-art library design and implementation? Look to Boost. Want a glimpse of what C++ might look like in the future? Look to Boost.

Boost is both a community of C++ developers and a collection of freely downloadable C++ libraries. Its web site is You should bookmark it now.

There are many C++ organizations and web sites, of course, but Boost has two things going for it that no other organization can match. First, it has a uniquely close and influential relationship with the C++ standardization committee. Boost was founded by committee members, and there continues to be strong overlap between the Boost and committee memberships. In addition, Boost has always had as one of its goals to act as a testing ground for capabilities that could be added to Standard C++. One result of this relationship is that of the 14 new libraries introduced into C++ by TR1 (see Item 54), more than two-thirds are based on work done at Boost.

The second special characteristic of Boost is its process for accepting libraries. It's based on public peer review. If you'd like to contribute a library to Boost, you start by posting to the Boost developers mailing list to gauge interest in the library and initiate the process of preliminary examination of your work. Thus begins a cycle that the web site summarizes as "Discuss, refine, resubmit. Repeat until satisfied."

Eventually, you decide that your library is ready for formal submission. A review manager confirms that your library meets Boost's minimal requirements. For example, it must compile under at least two compilers (to demonstrate nominal portability), and you have to attest that the library can be made available under an acceptable license (e.g., the library must allow free commercial and non-commercial use). Then your submission is made available to the Boost community for official review. During the review period, volunteers go over your library materials (e.g., source code, design documents, user documentation, etc.) and consider questions such as these:

  • How good are the design and implementation?

  • Is the code portable across compilers and operating systems?

  • Is the library likely to be of use to its target audience, i.e., people working in the domain the library addresses?

  • Is the documentation clear, complete, and accurate?

These comments are posted to a Boost mailing list, so reviewers and others can see and respond to one another's remarks. At the end of the review period, the review manager decides whether your library is accepted, conditionally accepted, or rejected.

Peer reviews do a good job of keeping poorly written libraries out of Boost, but they also help educate library authors in the considerations that go into the design, implementation, and documentation of industrial-strength cross-platform libraries. Many libraries require more than one official review before being declared worthy of acceptance.

Boost contains dozens of libraries, and more are added on a continuing basis. From time to time, some libraries are also removed, typically because their functionality has been superseded by a newer library that offers greater functionality or a better design (e.g., one that is more flexible or more efficient).

The libraries vary widely in size and scope. At one extreme are libraries that conceptually require only a few lines of code (but are typically much longer after support for error handling and portability is added). One such library is Conversion, which provides safer or more convenient cast operators. Its numeric_cast function, for example, throws an exception if converting a numeric value from one type to another leads to overflow or underflow or a similar problem, and lexical_cast makes it possible to cast any type supporting operator<< into a string — very useful for diagnostics, logging, etc. At the other extreme are libraries offering such extensive capabilities, entire books have been written about them. These include the Boost Graph Library (for programming with arbitrary graph structures) and the Boost MPL Library ("metaprogramming library").

Boost's bevy of libraries addresses a cornucopia of topics, grouped into over a dozen general categories. Those categories include:

  • String and text processing, including libraries for type-safe printf-like formatting, regular expressions (the basis for similar functionality in TR1 — see Item 54), and tokenizing and parsing.

  • Containers, including libraries for fixed-size arrays with an STL-like interface (see Item 54), variable-sized bitsets, and multidimensional arrays.

  • Function objects and higher-order programming, including several libraries that were used as the basis for functionality in TR1. One interesting library is the Lambda library, which makes it so easy to create function objects on the fly, you're unlikely to realize that's what you're doing:

    using namespace boost::lambda;                    // make boost::lambda
                                                      // functionality visible
    std::vector<int> v;
    std::for_each(v.begin(), v.end(),                 // for each element x in
                  std::cout << _1 * 2 + 10 	<< "\n");  // v, print x*2+10;
                                                      // "_1" is the Lambda
                                                      // library's placeholder
                                                      // for the current element

  • Generic programming, including an extensive set of traits classes. (See Item 47 for information on traits).

  • Template metaprogramming (TMP — see Item 48), including a library for compile-time assertions, as well as the Boost MPL Library. Among the nifty things in MPL is support for STL-like data structures of compile-time entities like types, e.g.,

    // create a list-like compile-time container of three types (float,
    // double, and long double) and call the container "floats"
    typedef boost::mpl::list<float, double, long double> floats;
    // create a new compile-time list of types consisting of the types in
    // "floats" plus "int" inserted at the front; call the new container "types"
    typedef boost::mpl::push_front<floats, int>::type types;

    Such containers of types (often known as typelists, though they can also be based on an mpl::vector as well as an mpl::list) open the door to a wide range of powerful and important TMP applications.

  • Math and numerics, including libraries for rational numbers; octonions and quaternions; greatest common divisor and least common multiple computations; and random numbers (yet another library that influenced related functionality in TR1).

  • Correctness and testing, including libraries for formalizing implicit template interfaces (see Item 41) and for facilitating test-first programming.

  • Data structures, including libraries for type-safe unions (i.e., storing variant "any" types) and the tuple library that led to the corresponding TR1 functionality.

  • Inter-language support, including a library to allow seamless interoperability between C++ and Python.

  • Memory, including the Pool library for high-performance fixed-size allocators (see Item 50); and a variety of smart pointers (see Item 13), including (but not limited to) the smart pointers in TR1. One such non-TR1 smart pointer is scoped_array, an auto_ptr-like smart pointer for dynamically allocated arrays; Item 44 shows an example use.

  • Miscellaneous, including libraries for CRC checking, date and time manipulations, and traversing file systems.

Remember, that's just a sampling of the libraries you'll find at Boost. It's not an exhaustive list.

Boost offers libraries that do many things, but it doesn't cover the entire programming landscape. For example, there is no library for GUI development, nor is there one for communicating with databases. At least there's not now — not as I write this. By the time you read it, however, there might be. The only way to know for sure is to check. I suggest you do it right now: Even if you don't find exactly what you're looking for, you're certain to find something interesting there.

Things to Remember

  • Boost is a community and web site for the development of free, open source, peer-reviewed C++ libraries. Boost plays an influential role in C++ standardization.

  • Boost offers implementations of many TR1 components, but it also offers many other libraries, too.

     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows