Mastering C++ Programming Modern C++ 17 at your fingertips Jeganathan Swaminathan BIRMINGHAM - MUMBAI Mastering C++ Programming Copyright © 2017 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: August 2017 Production reference: 1300817 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-162-9 www.packtpub.com Credits Author Copy Editor Jeganathan Swaminathan Gladson Monteiro Muktikant Garimella Reviewer Project Coordinator Brandon James Vaidehi Sawant Commissioning Editor Proofreader Aaron Lazar Safis Editing Acquisition Editor Indexer Denim Pinto Rekha Nair Content Development Editor Graphics Anurag Ghogre Abhinash Sahu Technical Editor Production Coordinator Madhunikita Sunil Chindarkar Melwyn Dsa About the Author Jeganathan Swaminathan, Jegan for short, is a freelance software consultant and founder of TekTutor, with over 17 years of IT industry experience. In the past, he has worked for AMD, Oracle, Siemens, Genisys Software, Global Edge Software Ltd, and PSI Data Systems. He has consulted for Samsung WTD (South Korea) and National Semiconductor (Bengaluru). He now works as a freelance external consultant for Amdocs (India). He works as freelance software consultant and freelance corporate trainer. He holds CSM, CSPO, CSD, and CSP certifications from Scrum Alliance. He is a polyglot software professional and his areas of interest include a wide range of C++, C#, Python, Ruby, AngularJS, Node.js, Kubernetes, Ansible, Puppet, Chef, and Java technologies. He is well known for JUnit, Mockito, PowerMock, gtest, gmock, CppUnit, Cucumber, SpecFlow, Qt, QML, POSIX – Pthreads, TDD, BDD, ATDD, NoSQL databases (MongoDB and Cassandra), Apache Spark, Apache Kafka, Apache Camel, Dockers, Continuous Integration (CI), Continuous Delivery (CD), Maven, Git, cloud computing, and DevOps. You can reach him for any C++, Java, Qt, QML, TDD, BDD, and DevOps-related training or consulting assignments. Jegan is a regular speaker at various technical conferences. I would like to extend my special thanks to the book's editor, Anurag Ghogre, for his tireless efforts to bring this book to completion, and Denim Pinto for approaching me to write this book. About the Reviewer Brandon James is a support escalation engineer who works with troubleshooting, debugging, and implementing identity management solutions for many enterprise customers for both on-premise and cloud solutions. He worked as a technical reviewer on the Microsoft Identity Manager 2016 Handbook by Packt Publishing. Previously, he worked as a Web Site/Web Application Developer designing and developing websites and internal web applications for various enterprise customers. He holds a B.S. in computer engineering and an M.S. in computer science. www.PacktPub.com 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. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. 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 Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1786461625. If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! This book is dedicated to my wife, Meenakshi, and my lovely children, Nitesh and Sriram. Table of Contents Preface 1 Chapter 1: C++17 Features 8 C++17 background 8 What's new in C++17? 9 What features are deprecated or removed in C++17? 10 Key features in C++17 10 Easier nested namespace syntax 10 New rules for type auto-detection from braced initializer list 12 Simplified static_assert 13 The std::invoke( ) method 14 Structured binding 15 If and Switch local scoped variables 16 Template type auto-deduction for class templates 17 Inline variables 18 Summary 19 Chapter 2: Standard Template Library 20 The Standard Template Library architecture 20 Algorithms 21 Iterators 21 Containers 24 Functors 24 Sequence containers 26 Array 26 Code walkthrough 27 Commonly used APIs in an array 27 Vector 29 Code walkthrough 30 Commonly used vector APIs 31 Code walkthrough 32 Pitfalls of a vector 33 List 33 Commonly used APIs in a list 36 Forward list 36 Code walkthrough 38 Commonly used APIs in a forward_list container 38