ebook img

Advanced C++ Programming Cookbook: Become an expert C++ programmer by mastering concepts like templates, concurrency, and type deduction PDF

443 Pages·2020·39.228 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Advanced C++ Programming Cookbook: Become an expert C++ programmer by mastering concepts like templates, concurrency, and type deduction

Advanced C++ Programming Cookbook Become an expert C++ programmer by mastering concepts like templates, concurrency, and type deduction Dr. Rian Quinn BIRMINGHAM - MUMBAI Advanced C++ Programming Cookbook Copyright © 2020 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 or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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. Commissioning Editor: Richa Tripathi Acquisition Editor: Shriram Shekhar Content Development Editor: Tiksha Sarang Senior Editor: Storm Mann Technical Editor: Romy Dias Copy Editor: Safis Editing Project Coordinator: Francy Puthiry Proofreader: Safis Editing Indexer: Rekha Nair Production Designer: Jyoti Chauhan First published: January 2020 Production reference: 1290120 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-83855-991-5 www.packt.com To my kids, embrace the challenges that others deem too hard to take on; be patient, strong, and confident, never letting anyone stand in your way. – Dad Packt.com Subscribe to our online digital library for full access to over 7,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Fully searchable for easy access to vital information Copy and paste, print, and bookmark content 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.packt.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.packt.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. Contributors About the author Dr. Rian Quinn is the Chief Technology Officer (CTO) in the Advanced Technologies Business Unit at Assured Information Security, Inc., having focused on trusted computing, hypervisor-related technologies, machine learning/artificial intelligence, and cybersecurity for more than 10 years, and has 9 years of technical management and business development experience. He holds a Ph.D. in computer engineering, with specializations in information assurance and computer architectures, from Binghamton University. He is the cofounder and lead developer of the Bareflank hypervisor, and is an active member of several open source projects, including Microsoft's Guideline Support Library (GSL) and OpenXT. Rian previously wrote Hands-On System Programming with C++. About the reviewers Sergey Gomon started his journey in IT 12 years ago at Belarus State University of Informatics and Radioelectronics, in the Artificial Intelligence department. He has about 8 years of industrial programming experience using C++ in several fields, such as network programming, information security, and image processing. He currently works at SolarWinds MSP and is an activist in the CoreHard C++ community. Zhuo Qingliang (also known as KDr2 online) is presently working at paodingai.com, a start-up FinTech company in China that is dedicated to improving the financial industry by using artificial intelligence technologies. He has over 10 years of experience in Linux, C, C++, Python, Perl, and Java development. He is interested in programming, doing consulting work, and participating in and contributing to the open source community (including the Julia community, of course). Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Getting Started with Library Development 7 Technical requirements 8 Understanding the principle of least surprise 8 Getting ready 8 How to do it... 9 How it works... 10 Example 1 10 Example 2 11 Example 3 11 Example 4 12 Example 5 13 Example 6 14 Example 7 15 Example 8 15 Example 9 16 How to namespace everything 17 Getting ready 17 How to do it... 17 How it works... 18 Example 1 18 Example 2 19 Header-only libraries 20 Getting ready 20 How to do it... 21 How it works... 22 How to handle includes 22 Global variables 24 Issues with C-style macros 26 How to implement a large library as header-only 27 Learning library development best practices 29 Getting ready 29 How to do it... 30 How it works... 30 What about warnings? 30 Static and dynamic analysis 31 Documentation 33 CII Best Practices 34 Learning how to use the boost APIs 34 Getting ready 34 How to do it... 35 Table of Contents How it works... 35 Example 1 36 Example 2 36 See also 37 Chapter 2: Using Exceptions for Error Handling 38 Technical requirements 39 Using the noexcept specifier 39 Getting ready 39 How to do it... 40 How it works... 41 Using the noexcept operator 46 Getting ready 47 How to do it... 47 How it works... 48 Using RAII 54 Getting ready 54 How to do it... 55 How it works... 56 Learning why to never throw exceptions in destructors 62 Getting ready 62 How to do it... 62 How it works... 63 Easily creating your own exception classes 68 Getting ready 68 How to do it... 68 How it works... 69 Chapter 3: Implementing Move Semantics 72 Technical requirements 73 Using compiler-generated special class member functions and the Big Five 73 Getting ready 73 How to do it... 74 How it works... 74 Making your class movable 77 Getting ready 78 How to do it... 78 How it works... 78 Move-only types 83 Getting ready 83 How to do it... 84 How it works... 84 Implementing the noexcept move constructor 88 Getting ready 89 How to do it... 89 [ ii ] Table of Contents How it works... 90 Learning to be wary of const&& 92 Getting ready 92 How to do it... 93 How it works... 93 Referencing qualified member functions 95 Getting ready 95 How to do it... 95 How it works... 96 Exploring objects that cannot be moved or copied 98 Getting ready 98 How to do it... 98 How it works... 99 Chapter 4: Using Templates for Generic Programming 102 Technical requirements 103 Implementing SFINAE 103 Getting ready 103 How to do it... 104 How it works... 105 Learning perfect forwarding 112 Getting ready 112 How to do it... 112 How it works... 113 Using if constexpr 117 Getting ready 117 How to do it... 118 How it works... 118 Using tuples to work with parameter packs 121 Getting ready 121 How to do it... 121 How it works... 122 Using type traits to overload functions and objects 127 Getting ready 128 How to do it... 128 How it works... 129 Learning how to implement template<auto> 133 Getting ready 134 How to do it... 134 How it works... 135 Working with explicit template declarations 137 Getting ready 137 How to do it... 137 How it works... 138 Chapter 5: Concurrency and Synchronization 141 [ iii ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.