What Is Boost? Boost Your C++ Programming With The Boost Libraries
For C++ programmers all over the world, Boost and the Boost libraries have become indispensable
By: Björn Karlsson
Dec. 21, 2005 08:00 PM
For C++ programmers all over the world, Boost and the Boost libraries have become indispensable. Weighing in at 60 C++ libraries, Boost is a large collection of peer-reviewed code covering a wide range of domains. But why should you care about that? Well, because Boost can quickly become indispensable for you too.
It all started in 1998 when Beman Dawes, a member of the C++ Standards Committee, founded Boost as a proving ground for new C++ libraries. C++ and the C++ Standard Library had just become standardized in ISO/IEC 14882-98, but that didn't mean that the work was done. So a community was formed and right from the start Boost managed to attract luminaries who shared the idea that Open Source C++ libraries were key to the future success of C++ and that they needed to be proven in the field before being proposed for inclusion in the standard. Today the success of Boost is apparent when you look at the contents of TR1 (the first technical report containing C++ library extensions). Almost all of the libraries come from Boost. Those libraries include support for regular expressions, a type traits library, random number generation, tuples, and smart pointers.
With 60 libraries to choose from, it's helpful to see some examples of Boost's contents. One of the most widespread and popular libraries is Boost.Smart_ptr, a collection of smart pointers that make resource handling in C++ much less error prone. Another of my favorites is Boost.Bind, which creates function objects, optionally with bound arguments. When using the STL, Bind is a lifesaver that lets you create function objects on-the-fly. Boost.Regex brings regular expressions to C++, and supports both Perl-like expressions and the regex flavors from awk, grep, egrep, and emacs. For variant data types, there are two libraries in Boost: Boost.Any for unbounded variants and Boost.Variant for creating variants supporting a bounded set of types. For handling date and time calculations, there's Boost.Date_time. There's a portable threading library available called Boost.Thread. A compile-time analogue to the STL is available through Boost.Mpl, a library that makes the world of compile-time programming available even to us mortals. For manipulating mathematical graphs, there's Boost.Graph, which includes graph components and algorithms such as Dijkstra's shortest path, A* search, and topological sort. For delayed functions, there's a library called Boost.Function. If you need flexible callbacks, i.e., signals and slots, there's Boost.Signal. Do you need some string-related algorithm that's not available in the C++ Standard Library? See if Boost.String_algo has got what you need. Are you tired of platform-specific hacks for working with files and directories? Try Boost.Filesystem.
As you can see, the Boost libraries cover a wide range of domains. Visit www.boost.org for a complete listing, and while you're there, why not download the libraries? They're expertly designed, thoroughly tested, well documented, and free of charge.
The Boost libraries are released under the Boost License - or a license that complies with the Boost license requirements - and they are free for both commercial and non-commercial use. The big differences when compared with the GNU General Public License (GPL) are that derivative works don't have to release their source code, and that the Boost license only affects the Boost libraries, whereas the GPL also affects the code that uses GPL'd libraries. The relaxed license exists both for philosophical and practical reasons; and because Boost libraries often aim for inclusion in the C++ standard, they have to be free.
Boost is popular both in commercial and non-commercial projects, and is being used more and more in Linux distributions. Currently Boost comes bundled with Fedora, Debian, and NetBSD. Of course, the libraries are also available for download from www.boost.org. You'll also find that many best practices books are starting to talk about the Boost libraries and recommend that you use them, for example Scott Meyers' Effective C++.
There are several books of interest for anyone wanting to learn more about Boost. The first book published about Boost was The Boost Graph Library User Guide and Reference Manual, which, true to its name, talks about the ins and outs of the Graph library. Then came C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond about template metaprogramming with the fantastic Mpl library. Finally, there's the recently published Beyond the C++ Standard Library: An Introduction to Boost, written by yours truly. The book briefly outlines all the Boost libraries, and provides in-depth coverage of 12 important ones. I hope that we'll see many more books about Boost in the future.
It's worth noting that the online documentation for the Boost libraries is very good.
I know that you're eager to see some code now, and I've chosen a short example program from my Boost book that shows how to use Boost.Regex to change the British spelling of the word "colour" to the American "color". The problem, of course, is that you have to account not only for the word "color," but "colors," "colorize," and so on:
First we create a regular expression of type boost::regex, which holds the regex. It's a case-insensitive expression. The regex simply captures the word "colour" in three marked sub-expressions, which we need in order to surgically remove the letter "u" (the second sub-expression). Then, we apply the algorithm regex_replace() on a std::string, and use the format string $1$3, which corresponds to the first and third sub-expressions of the regex; here, everything but the letter "u." Finally, the modified string, s, is printed to std::cout to demonstrate that all traces of the British spelling are gone. That's just a short example to display some of the power of Boost.Regex. If you want more, I think you know what you need to do.
Before concluding this article, I'd like to tell you a little more about what's new at Boost. There are several libraries waiting in the review queue, which means that they've passed the first hurdle, which is to create interest among the Boosters. Of course, for a library to be submitted to Boost, it has to be more than just interesting and useful; it must be a complete package of software adhering to the Boost guidelines, and it must come with documentation. For any library to become accepted by Boost, it must undergo peer review. Let's take a quick look at some libraries that have recently been accepted into Boost. Boost.Foreach is a very handy macro that makes it simple to loop over the elements of all kinds of collections; Boost.Typeof provides an approximation of the forthcoming (and long-awaited!) language support for auto and typeof, which are mechanisms for querying the type of an expression; Boost.Statechart is a powerful library for creating finite state machines; Boost.Xpressive is a regular expression template library - essentially a bridge between regular expressions (see Boost.Regex) and the parser generator framework (see Boost.Spirit). If you're interested in knowing more about these libraries and other potential additions to Boost - and perhaps propose your own for inclusion - you'll want to join the Boost mailing lists, which are also accessible through newsgroup interfaces.
Only the future will tell which new libraries will make it into Boost, but rest assured that lively discussions on new potential additions to Boost will always be common on the mailing lists.
I hope that this introduction to Boost has whetted your appetite for this interesting community and collection of useful C++ libraries. I also hope to see you at www.boost.org and on the Boost mailing lists.
I'll leave you with a quote from Bjarne Stroustrup in an interview by Bill Venners. The topic was C++ programming styles, and the question was how to elevate one's level of programming, or, wittily, "how to climb above C-level":
"To get out of writing low-level code, you needn't start writing a lot of classes. Instead, start using facilities provided in libraries."
Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week