📄 Page
1
C ++ C R A S H C O U R S E J O S H L O S P I N O S O A F A S T - P A C E D I N T R O D U C T I O N O P T I M I Z E D C O M P I L E D A N D M O D E R N C + + , C++ is one of the most widely used languages for real-world software. In the hands of a knowledgeable programmer, C++ can produce small, efficient, and readable code that any programmer would be proud of. Written for intermediate to advanced programmers, C++ Crash Course cuts through the weeds to get straight to the core of C++17, the most modern revision of the ISO standard. Part I covers the core C++ language, from types and functions to the object life cycle and expressions. Part II introduces the C++ Standard Library and Boost Libraries, where you’ll learn about special utility classes, data structures, and algorithms, as well as how to manipulate file systems and build high-performance programs that communicate over networks. You’ll learn all the major features of modern C++, including: • Fundamental types, reference types, and user- defined types • Compile-time polymorphism with templates and runtime polymorphism with virtual classes • The object lifecycle including storage duration, call stacks, memory management, exceptions, and the RAII (resource acquisition is initialization) paradigm • Advanced expressions, statements, and functions • Smart pointers, data structures, dates and times, numerics, and probability/statistics facilities • Containers, iterators, strings, and algorithms • Streams and files, concurrency, networking, and application development With well over 500 code samples and nearly 100 exercises, C++ Crash Course is sure to help you build a strong C++ foundation. A B O U T T H E A U T H O R Josh Lospinoso served for 15 years in the US Army and built the C++ course used by the US Cyber Command to teach its junior developers. He has published over 20 peer-reviewed articles and co-founded a successfully acquired security company. A Rhodes Scholar, Lospinoso holds a PhD in Statistics from the University of Oxford. www.nostarch.com TH E F I N EST I N G E E K E NTE RTA I N M E NT ™ SHELVE IN: PROGRAM M ING LANGUAGES/C++ $59.95 ($78.95 CDN) Covers C++17 C + + C R A S H C O U R S E C + + C R A S H C O U R S E L O S P IN O S O
📄 Page
2
(This page has no text content)
📄 Page
3
C++ CRASH COURSE
📄 Page
4
(This page has no text content)
📄 Page
5
C + + C R A S H C O U R S E A F a s t - P a c e d I n t r o d u c t i o n by Josh Lospinoso San Francisco
📄 Page
6
C++ CRASH COURSE. Copyright © 2019 by Josh Lospinoso. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-10: 1-59327-888-8 ISBN-13: 978-1-59327-888-5 Publisher: William Pollock Production Editors: Meg Sneeringer and Riley Hoffman Cover Illustration: Josh Ellingson Interior Design: Octopod Studios Developmental Editors: Chris Cleveland and Patrick DiJusto Technical Reviewer: Kyle Willmon Copyeditor: Anne Marie Walker Compositors: Happenstance Type-O-Rama, Riley Hoffman, and Meg Sneeringer Proofreader: Paula L. Fleming For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900; info@nostarch.com www.nostarch.com Library of Congress Cataloging-in-Publication Data Names: Lospinoso, Josh, author. Title: C++ crash course : a fast-paced introduction / Josh Lospinoso. Description: First edition. | San Francisco, CA : No Starch Press, Inc., [2019] Identifiers: LCCN 2019020529 (print) | LCCN 2019022057 (ebook) | ISBN 9781593278892 (epub) | ISBN 1593278896 (epub) | ISBN 9781593278885 (print) | ISBN 1593278888 (print) Subjects: LCSH: C++ (Computer program language) | Computer programming. Classification: LCC QA76.73.C153 (ebook) | LCC QA76.73.C153 L67 2019 (print) | DDC 005.13/3--dc23 LC record available at https://lccn.loc.gov/2019020529 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
📄 Page
7
#include <algorithm> #include <iostream> #include <string> int main() { auto i{ 0x01B99644 }; std::string x{ " DFaeeillnor" }; while (i--) std::next_permutation(x.begin(), x.end()); std::cout << x; }
📄 Page
8
(This page has no text content)
📄 Page
9
About the Author Josh Lospinoso, PhD, is an entrepreneur who served 15 years in the US Army. As a cyber officer, Josh wrote dozens of infosec tools and taught C++ to junior developers. He has spoken at a wide range of conferences, published over 20 peer-reviewed articles, is a Rhodes Scholar, and holds a patent. In 2012, he co-founded a successfully acquired security company. He keeps a blog and is an active contributor to open source software. About the Technical Reviewer Kyle Willmon is a systems developer with 12 years of C++ experience. He has worked in the information security community for 7 years utilizing C++, Python, and Go across a variety of projects. Kyle currently works as a developer for Sony’s Global Threat Emulation Team.
📄 Page
10
(This page has no text content)
📄 Page
11
B R I E F C O N T E N T S Foreword by Peter Bindels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxix Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxi An Overture to C Programmers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii PART I: THE C++ CORE LANGUAGE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Chapter 1: Up and Running . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Chapter 2: Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Chapter 3: Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Chapter 4: The Object Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Chapter 5: Runtime Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Chapter 6: Compile-Time Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Chapter 7: Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Chapter 8: Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Chapter 9: Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 PART II: C++ LIBRARIES AND FRAMEWORKS . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Chapter 10: Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 Chapter 11: Smart Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 Chapter 12: Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 Chapter 13: Containers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407 Chapter 14: Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463 Chapter 15: Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 Chapter 16: Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
📄 Page
12
x Brief Contents Chapter 17: Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551 Chapter 18: Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 573 Chapter 19: Concurrency and Parallelism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 639 Chapter 20: Network Programming with Boost Asio . . . . . . . . . . . . . . . . . . . . . . . . . 663 Chapter 21: Writing Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 691 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
📄 Page
13
C O N T E N T S I N D E T A I L FOREWORD by Peter Bindels xxv ACKNOWLEDGMENTS xxix INTRODUCTION xxxi About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxii Who Should Read This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii What’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii Part I: The C++ Core Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiii Part II: C++ Libraries and Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxiv AN OVERTURE TO C PROGRAMMERS xxxvii Upgrading to Super C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix Function Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xl auto Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xlii Namespaces and Implicit typedef of struct, union, and enum . . . . . . . . . . . . . xliii Intermingling C and C++ Object Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xlv C++ Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xlvi Expressing Ideas Concisely and Reusing Code . . . . . . . . . . . . . . . . . . . . . . . xlvi The C++ Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xlviii Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xlix Generic Programming with Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . l Class Invariants and Resource Management . . . . . . . . . . . . . . . . . . . . . . . . . . . li Move Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . lv Relax and Enjoy Your Shoes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . lvi PART I: THE C++ CORE LANGUAGE 1 1 UP AND RUNNING 3 The Structure of a Basic C++ Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Creating Your First C++ Source File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Main: A C++ Program’s Starting Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Libraries: Pulling in External Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 The Compiler Tool Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Setting Up Your Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Windows 10 and Later: Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
📄 Page
14
xii Contents in Detail macOS: Xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Linux and GCC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Bootstrapping C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The C++ Type System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Initializing a Variable’s State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 printf Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Revisiting step_function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 Xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 GCC and Clang Debugging with GDB and LLDB . . . . . . . . . . . . . . . . . . . . . 25 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2 TYPES 31 Fundamental Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Integer Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Character Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Boolean Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 The std::byte Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 The size_t Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Accessing Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 A Nickel Tour of for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 C-Style Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Enumeration Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Plain-Old-Data Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Fully Featured C++ Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Access Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 The Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 3 REFERENCE TYPES 67 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Addressing Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Dereferencing Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
📄 Page
15
Contents in Detail xiii The Member-of-Pointer Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Pointers and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Pointers Are Dangerous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 void Pointers and std::byte Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 nullptr and Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Usage of Pointers and References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Forward-Linked Lists: The Canonical Pointer-Based Data Structure . . . . . . . . . . 78 Employing References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 this Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 const Correctness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 const Member Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Member Initializer Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 auto Type Deduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Initialization with auto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 auto and Reference Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 auto and Code Refactorings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 4 THE OBJECT LIFE CYCLE 89 An Object’s Storage Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Allocation, Deallocation, and Lifetime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Automatic Storage Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Static Storage Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Thread-Local Storage Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Dynamic Storage Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Tracing the Object Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 The throw Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Using try-catch Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 stdlib Exception Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 User-Defined Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 The noexcept Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Call Stacks and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 A SimpleString Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Appending and Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Using SimpleString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Composing a SimpleString . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Call Stack Unwinding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Exceptions and Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Alternatives to Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Copy Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Copy Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Copy Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Default Copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Copy Guidelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
📄 Page
16
xiv Contents in Detail Move Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Copying Can Be Wasteful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Value Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 lvalue and rvalue References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 The std::move Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Move Construction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Move Assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 The Final Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Compiler-Generated Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 5 RUNTIME POLYMORPHISM 133 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 A Motivating Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Adding New Loggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 Object Composition and Implementation Inheritance . . . . . . . . . . . . . . . . . . 137 Defining Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Base Class Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 Member Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 virtual Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Pure-Virtual Classes and Virtual Destructors . . . . . . . . . . . . . . . . . . . . . . . . . 142 Implementing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Using Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Updating the Bank Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Constructor Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Property Injection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 Choosing Constructor or Property Injection . . . . . . . . . . . . . . . . . . . . . . . . 146 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 6 COMPILE-TIME POLYMORPHISM 149 Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Declaring Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Template Class Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Template Function Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Instantiating Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Named Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 const_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 static_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 reinterpret_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 narrow_cast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 mean: A Template Function Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Genericizing mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Template Type Deduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158 SimpleUniquePointer: A Template Class Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Type Checking in Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
📄 Page
17
Contents in Detail xv Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Defining a Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Type Traits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Building Concepts from Requires Expressions . . . . . . . . . . . . . . . . . . . . . . . 167 Using Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Ad Hoc Requires Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 static_assert: The Preconcepts Stopgap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Non-Type Template Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Variadic Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Advanced Template Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Template Specialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Name Binding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Type Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Template Metaprogramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Template Source Code Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Polymorphism at Runtime vs . Compile Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 7 EXPRESSIONS 181 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Member Access Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Ternary Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 The Comma Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Operator Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Overloading Operator new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Operator Precedence and Associativity . . . . . . . . . . . . . . . . . . . . . . . . . . . 194 Evaluation Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 User-Defined Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Implicit Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Explicit Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 C-Style Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 User-Defined Type Conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Constant Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 A Colorful Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 The Case for constexpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Volatile Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 8 STATEMENTS 211 Expression Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Compound Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
📄 Page
18
xvi Contents in Detail Declaration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Type Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220 Structured Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Selection Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 if Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 switch Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Iteration Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230 do-while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Ranged-Based for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Jump Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 break Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 continue Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 goto Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 9 FUNCTIONS 243 Function Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Prefix Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 Suffix Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 auto Return Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 auto and Function Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Overload Resolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Variadic Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Variadic Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Programming with Parameter Packs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Revisiting the sum Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Fold Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Declaring a Function Pointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Type Aliases and Function Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 The Function-Call Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 A Counting Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Lambda Parameters and Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Default Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Generic Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Lambda Return Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Lambda Captures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 constexpr Lambda Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 std::function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Declaring a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 An Extended Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
📄 Page
19
Contents in Detail xvii The main Function and the Command Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 The Three main Overloads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 Exploring Program Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 A More Involved Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Exit Status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 PART II: C++ LIBRARIES AND FRAMEWORKS 279 10 TESTING 281 Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Integration Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Acceptance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Performance Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 An Extended Example: Taking a Brake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Implementing AutoBrake . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Test-Driven Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Adding a Service-Bus Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 Unit-Testing and Mocking Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 The Catch Unit-Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304 Google Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 Boost Test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317 Summary: Testing Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 Mocking Frameworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Google Mock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 HippoMocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 A Note on Other Mocking Options: FakeIt and Trompeloeil . . . . . . . . . . . . . 337 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 11 SMART POINTERS 341 Smart Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 Smart Pointer Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Scoped Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Constructing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342 Bring in the Oath Breakers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343 Implicit bool Conversion Based on Ownership . . . . . . . . . . . . . . . . . . . . . . 344 RAII Wrapper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Pointer Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 Comparison with nullptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 Swapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 Resetting and Replacing a scoped_ptr . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 Non-transferability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 boost::scoped_array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348 A Partial List of Supported Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
📄 Page
20
xviii Contents in Detail Unique Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Constructing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Supported Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Transferable, Exclusive Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350 Unique Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Deleters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 352 Custom Deleters and System Programming . . . . . . . . . . . . . . . . . . . . . . . . . 352 A Partial List of Supported Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 Shared Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 Constructing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Specifying an Allocator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 Supported Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357 Transferable, Non-Exclusive Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Shared Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358 Deleters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 A Partial List of Supported Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Weak Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Constructing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Obtaining Temporary Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 Advanced Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Supported Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362 Intrusive Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363 Summary of Smart Pointer Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 Allocators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 12 UTILITIES 369 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 tribool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 pair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 any . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 variant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 Date and Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Boost DateTime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 Chrono . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 Numerics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Numeric Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 Mathematical Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394 Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396 Numeric Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Boost Numeric Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401 Compile-Time Rational Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405