Book Image

Learning Boost C++ Libraries

By : Arindam Mukherjee
Book Image

Learning Boost C++ Libraries

By: Arindam Mukherjee

Overview of this book

Table of Contents (19 chapters)
Learning Boost C++ Libraries
About the Author
About the Reviewers


Boost is not just a collection of useful, portable, generic C++ libraries. It is an important incubator for ideas and concepts that make their way to the ISO C++ Standard itself. If you are involved in the development of software written in C++, then learning to use the Boost libraries would save you from reinventing the wheel, improve the quality of your software, and very likely push up your productivity.

I first came across the Boost libraries a decade ago, while looking for a portable C++ regular expressions library. Over the next couple of days, porting Perl and Korn Shell text-processing code to C++ became a breeze, and I took an instant liking to Boost. In using many more Boost libraries to write software since then, I often found myself digging deep into the documentation, or asking questions on the mailing list and online forums to understand library semantics and nuances. As effective as that was, I always sorely missed a book that would get me started on the most useful Boost libraries and help me become productive faster. This is that book.

Boost has a wide array of libraries for solving various kinds of programming tasks. This book is a tutorial introduction to a selection of over of the most useful libraries from Boost to solve programming problems effectively. The chosen libraries represent the breadth of cross-cutting concerns from software development, including data structures and algorithms, text processing, memory management, exception safety, date and time calculations, file and directory management, concurrency, and file and network I/O, among others. You will learn about each library by understanding the kind of problems it helps solve, learning the fundamental concepts associated with it, and looking at a series of code examples to understand how the library is used. Libraries introduced earlier in this book are freely used in later examples, exposing you to the frequent synergies that occur in practice between the Boost libraries.

As a collection of peer-reviewed, open source libraries, Boost draws heavily from community expertise. I firmly believe that this book will give you a strong practical foundation in using the Boost libraries. This foundation will reflect in the quality of the software you write, and also give you the leverage to engage with the Boost community and make valuable contributions to it.

What this book covers

Chapter 1, Introducing Boost, discusses how to set up a development environment to use the Boost libraries. We cover different ways of obtaining Boost library binary packages, building them from source for different configurations, and using them in a development environment.

Chapter 2, The First Brush with Boost's Utilities, explores a handful of Boost libraries for common programming tasks that include dealing with variant data types, handling command-line arguments, and detecting the configuration parameters of the development environment.

Chapter 3, Memory Management and Exception Safety, explains what is meant by exception safety, and shows how to write exception-safe code using the different smart pointer types provided by Boost and C++11.

Chapter 4, Working with Strings, explores the Boost String Algorithms library for performing various computations with character strings, the Boost Range library for elegantly defining subsequences, the Boost Tokenizer library to split strings into tokens using different strategies, and the Boost Regex library to search for complex patterns in text.

Chapter 5, Effective Data Structures beyond STL, deals with the Boost Container library focusing on containers not available in the C++ Standard Library. We see the Pointer Container library for storing dynamically-allocated objects in action, and use the Boost Iterator library to generate various value sequences from underlying containers.

Chapter 6, Bimap and Multi-index Containers, looks at bidirectional maps and multi-index containers—two nifty container templates from Boost.

Chapter 7, Higher Order and Compile-time Programming, delves into compile-time programming using Boost Type Traits and Template Metaprogramming libraries. We take a first look at Domain Specific Embedded Languages and use Boost Phoenix to build basic expression templates. We use Boost Spirit to build simple parsers using the Spirit Qi DSEL.

Chapter 8, Date and Time Libraries, introduces the Boost Date Time and Boost Chrono libraries to represent dates, time points, intervals, and periods.

Chapter 9, Files, Directories, and IOStreams, features the Boost Filesystem library for manipulating filesystem entries, and the Boost IOStreams library for performing type-safe I/O with rich semantics.

Chapter 10, Concurrency with Boost, uses the Boost Thread library and Boost Coroutine library to write concurrent logic, and shows various synchronization techniques in action.

Chapter 11, Network Programming Using Boost Asio, shows techniques for writing scalable TCP and UDP servers and clients using the Asio library.

Appendix, C++11 Language Features Emulation, summarizes C++11 move semantics and Boost's emulation of several C++11 features in C++03.

What you need for this book

You will need a computer capable of running an operating system that supports a C++ compiler toolchain supported by Boost. You can find more details at

To compile and run the code from this book, you will need to install the Boost libraries version 1.56 or later. See Chapter 1, Introducing Boost, for more details.

Many code examples in this book require C++11 support, and thus, you should choose versions of your compiler that have good support for C++11. You can find more details at

A CMake project is provided with the downloadable source code to help you quickly build all the examples using your preferred build system (gmake or Microsoft Visual Studio). In order to use this, you need to install CMake version 2.8 or later. See Chapter 1, Introducing Boost, for more details.

This book tries not to repeat content from the online reference manual. You should use the Boost library's online reference manuals liberally in conjunction with this book to discover additional properties, functions, and techniques. You can find the documentation at

Finally, the code listings in this book are sometimes abridged for brevity and focus. The code examples accompanying this book are complete versions of these listings, and you should use them when trying to build the examples.

Who this book is for

This book is for every C++ programmer who is interested in learning about Boost. In particular, if you have never used the Boost libraries before, Learning Boost C++Libraries will get you up to speed with understanding, building, deploying, and using the Boost libraries. If you are familiar with the Boost libraries, but were looking for a springboard to dive deeper and take your expertise to the next level, this book will give you a comprehensive round-up of the most useful Boost libraries and the ways to use them in practical code.

Boost is a collection of C++ libraries, and naturally, C++ is the sole language used in this book. You need to have a good working knowledge of C++. In particular, you should be able to read code that uses C++ templates, understand the C++ compilation model, and be able to use a C++ development environment on Linux, Windows, or Mac OS.

This book does not cover general C++ concepts as a rule, but some useful C++ books and articles, listed at the end of some chapters, should serve as excellent references.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words and C++ language keywords in text are shown as follows: "We pass the number of bytes returned by async_receive to the handler."

Folder names, filenames, file extensions, pathnames, include file names in text are shown as follows: "The header file boost/asio.hpp includes most of the types and functions required for using the Asio library".

A block of code is set as follows:

46 int main() {
47   asio::io_service service;
48   UDPAsyncServer server(service, 55000);
50   boost::thread_group pool;
51   pool.create_thread([&service] {; });
52   pool.create_thread([&service] {; });
53   pool.join_all();
54 }

Except in smaller code snippets, each line of code is numbered for ease of reference from within the text. Important lines of code in a block are highlighted as shown above, and referred to from text using line numbers in parentheses (lines 51-52).

Any command-line input is written as follows:

$ g++ -g listing1.cpp -o listing1 -lboost_system -lboost_coroutine -lboost_date_time -std=c++11

Important new programming terms are shown in bold. Conceptual terms are shown in italics.


Important additional details about a topic appear like this, as in a sidebar.


Important notes, tips, and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.