Learning Boost C++ Libraries Solve practical programming problems using powerful, portable, and expressive libraries from Boost Arindam Mukherjee BIRMINGHAM - MUMBAI Learning Boost C++ Libraries Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: July 2015 Production reference: 1280715 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78355-121-7 www.packtpub.com Credits Author Project Coordinator Arindam Mukherjee Vijay Kushlani Reviewers Proofreaders Michael Medin Stephen Copestake Anthony Shoumikhin Safis Editing Drew Tennenbaum Indexer Sergey Zubkov Hemangini Bari Commissioning Editor Graphics Usha Iyer Sheetal Aute Acquisition Editor Production Coordinator Nikhil Karkal Komal Ramchandani Content Development Editors Cover Work Natasha DSouza Komal Ramchandani Sweny Sukumaran Technical Editors Pramod Kumavat Saurabh Malhotra Mitali Somaiya Copy Editor Rashmi Sawant About the Author Arindam Mukherjee is a senior principal software engineer at Symantec, Pune, India, where he is involved in the research and development of business continuity solutions for enterprises. He has used C++ extensively for developing large-scale distributed systems. He was a speaker at Dr. Dobb's Journal India Conference 2014 and is the organizer of regular meets for the Pune C++ and Boost Meetup. He believes that writing books and articles, speaking for interest groups, and engaging with the programming community are the best ways to develop a critical understanding of technology. He is also an amateur musician, dabbles in food photography, and loves profound discussions with his 4-year-old daughter, especially about dinosaurs and their diets. I would like to express my sincerest gratitude to Sergey Zubkov for helping me refine the content of this book with his critical reviews and observations. Special thanks to Anthony Shoumikhin, Drew Tennenbaum, and Michael Medin for their thoughtful reviews and feedback. A special word of thanks to Nikhil Karkal, Natasha DSouza, Pramod Kumavat, and Sweny Sukumaran at Packt Publishing for their tremendous support and helping me manage all the missed deadlines. Last but not least, I thank my father for making me believe as a child that writing books could be fun, my mother for far more than words can ever express, my wife for believing in my dream, and my daughter for making it all worthwhile. About the Reviewers Michael Medin is a senior developer and lead architect of the NSClient++ project. He is an avid C++ developer and has been developing open source software using C++ and the Boost library for longer than he cares to remember. As always, I would like to thank my beloved, Xiqun, for putting up with me when I spend countless hours working on NSClient++ and my two daughters for always bringing a smile to my face. Anthony Shoumikhin is yet another geek who loves hacking, cycling, swimming, and occasional work at Microsoft. He grew up in Ukraine and spent his early years in a city of rocket science and secret technologies—Dnipropetrovsk. These days, he works in Redmond, WA, on an upcoming release of Microsoft Office for Mac and iOS. In his spare time, he creates full-stack mobile apps and funny low-level system hacks on Mac OS X and Linux (mostly in his beloved C++ empowered with Boost). Drew Tennenbaum was introduced to programming at the age of 12. As a present, his parents gave him his first computer, a Commodore 64. A family friend purchased a book titled, Assembly Language for Kids: Commodore 64. Bored one night, he began reading the book and instantly found attraction in learning how to make a machine perform specific tasks. He quickly took to assembly language, which is now the foundation for much of what he works on today. He attended the University of Arizona, where he received a BS degree in Computer Science and Math. Since graduating from university in 1997, he has worked on a vast array of technologies, ranging from video games to embedded devices. He spent many years working on massively multiplayer online video games. In 2011, he was a Technical Director at BioWare, helping launch Star Wars: The Old Republic. More recently, he managed the development of the Appstore for Amazon's line of hardware devices, including the Kindle and Fire TV. In early 2015, he founded Titan Labs. Titan Labs is a small boutique consulting firm based in sunny Southern California. In his spare time, he enjoys riding one of his many motorcycles and also holds an amateur motorcycle racing license. I want to thank my parents for giving me the room to explore my passions in life. Without their patience, I would not have been where I am today, and more importantly, I would not have been the person I've become. I would also like to thank Dawn, my partner in life who supports me in anything I do and puts up with those late night programming sessions. Finally, I would like to thank all of my family members, including my grandmother, brother, and sister. Sergey Zubkov is a former biochemistry researcher who became a C++ programmer. He is currently working at Morgan Stanley and spends his free time updating http://cppreference.com. www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub. com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface ix Chapter 1: Introducing Boost 1 How it all started 1 What is Boost? 2 Getting started with Boost libraries 3 Necessary software 3 Linux toolchain 4 Windows toolchain 4 Obtaining and building Boost libraries 5 Planning your Boost sandbox 5 Library naming conventions 5 Library name components 5 Library name layouts 6 Installing a Boost binary distribution 8 Building and installing the Boost libraries from source 10 Using Boost libraries in your projects 15 Linking against Boost libraries on Linux 16 Linking against Boost libraries on Windows 17 Building the code listings in this book 20 CMake 20 Code examples 20 Self-test questions 23 Summary 23 Chapter 2: The First Brush with Boost's Utilities 25 Simple data structures 25 Boost.Optional 26 Accessing values stored in boost::optional 28 get_value_or 29 Boost.Optional versus pointers 30 [ i ]