Book description
Over 100 recipes to help you overcome your difficulties with C++ programming and gain a deeper understanding of the working of modern C++
About This Book
- Explore the most important language and library features of C++17, including containers, algorithms, regular expressions, threads, and more,
- Get going with unit testing frameworks Boost.Test, Google Test and Catch,
- Extend your C++ knowledge and take your development skills to new heights by making your applications fast, robust, and scalable.
Who This Book Is For
If you want to overcome difficult phases of development with C++ and leverage its features using modern programming practices, then this book is for you. The book is designed for both experienced C++ programmers as well as people with strong knowledge of OOP concepts.
What You Will Learn
- Get to know about the new core language features and the problems they were intended to solve
- Understand the standard support for threading and concurrency and know how to put them on work for daily basic tasks
- Leverage C++'s features to get increased robustness and performance
- Explore the widely-used testing frameworks for C++ and implement various useful patterns and idioms
- Work with various types of strings and look at the various aspects of compilation
- Explore functions and callable objects with a focus on modern features
- Leverage the standard library and work with containers, algorithms, and iterators
- Use regular expressions for find and replace string operations
- Take advantage of the new filesystem library to work with files and directories
- Use the new utility additions to the standard library to solve common problems developers encounter including string_view, any , optional and variant types
In Detail
C++ is one of the most widely used programming languages. Fast, efficient, and flexible, it is used to solve many problems. The latest versions of C++ have seen programmers change the way they code, giving up on the old-fashioned C-style programming and adopting modern C++ instead.
Beginning with the modern language features, each recipe addresses a specific problem, with a discussion that explains the solution and offers insight into how it works. You will learn major concepts about the core programming language as well as common tasks faced while building a wide variety of software. You will learn about concepts such as concurrency, performance, meta-programming, lambda expressions, regular expressions, testing, and many more in the form of recipes. These recipes will ensure you can make your applications robust and fast.
By the end of the book, you will understand the newer aspects of C++11/14/17 and will be able to overcome tasks that are time-consuming or would break your stride while developing.
Style and approach
This book follows a recipe-based approach, with examples that will empower you to implement the core programming language features and explore the newer aspects of C++.
Table of contents
- Preface
-
Learning Modern Core Language Features
- Introduction
- Using auto whenever possible
- Creating type aliases and alias templates
- Understanding uniform initialization
- Understanding the various forms of non-static member initialization
- Controlling and querying object alignment
- Using scoped enumerations
- Using override and final for virtual methods
- Using range-based for loops to iterate on a range
- Enabling range-based for loops for custom types
- Using explicit constructors and conversion operators to avoid implicit conversion
- Using unnamed namespaces instead of static globals
- Using inline namespaces for symbol versioning
- Using structured bindings to handle multi-return values
-
Working with Numbers and Strings
- Introduction
- Converting between numeric and string types
- Limits and other properties of numeric types
- Generating pseudo-random numbers
- Initializing all bits of internal state of a pseudo-random number generator
- Creating cooked user-defined literals
- Creating raw user-defined literals
- Using raw string literals to avoid escaping characters
- Creating a library of string helpers
- Verifying the format of a string using regular expressions
- Parsing the content of a string using regular expressions
- Replacing the content of a string using regular expressions
- Using string_view instead of constant string references
-
Exploring Functions
- Introduction
- Defaulted and deleted functions
- Using lambdas with standard algorithms
- Using generic lambdas
- Writing a recursive lambda
- Writing a function template with a variable number of arguments
- Using fold expressions to simplify variadic function templates
- Implementing higher-order functions map and fold
- Composing functions into a higher-order function
- Uniformly invoking anything callable
-
Preprocessor and Compilation
- Introduction
- Conditionally compiling your source code
- Using the indirection pattern for preprocessor stringification and concatenation
- Performing compile-time assertion checks with static_assert
- Conditionally compiling classes and functions with enable_if
- Selecting branches at compile time with constexpr if
- Providing metadata to the compiler with attributes
-
Standard Library Containers, Algorithms, and Iterators
- Introduction
- Using vector as a default container
- Using bitset for fixed-size sequences of bits
- Using vector<bool> for variable-size sequences of bits
- Finding elements in a range
- Sorting a range
- Initializing a range
- Using set operations on a range
- Using iterators to insert new elements in a container
- Writing your own random access iterator
- Container access with non-member functions
-
General Purpose Utilities
- Introduction
- Expressing time intervals with chrono::duration
- Measuring function execution time with a standard clock
- Generating hash values for custom types
- Using std::any to store any value
- Using std::optional to store optional values
- Using std::variant as a type-safe union
- Visiting a std::variant
- Registering a function to be called when a program exits normally
- Using type traits to query properties of types
- Writing your own type traits
- Using std::conditional to choose between types
-
Working with Files and Streams
- Introduction
- Reading and writing raw data from/to binary files
- Reading and writing objects from/to binary files
- Using localized settings for streams
- Using I/O manipulators to control the output of a stream
- Using monetary I/O manipulators
- Using time I/O manipulators
- Working with filesystem paths
- Creating, copying, and deleting files and directories
- Removing content from a file
- Checking the properties of an existing file or directory
- Enumerating the content of a directory
- Finding a file
-
Leveraging Threading and Concurrency
- Introduction
- Working with threads
- Handling exceptions from thread functions
- Synchronizing access to shared data with mutexes and locks
- Avoiding using recursive mutexes
- Sending notifications between threads
- Using promises and futures to return values from threads
- Executing functions asynchronously
- Using atomic types
- Implementing parallel map and fold with threads
- Implementing parallel map and fold with tasks
-
Robustness and Performance
- Introduction
- Using exceptions for error handling
- Using noexcept for functions that do not throw
- Ensuring constant correctness for a program
- Creating compile-time constant expressions
- Performing correct type casts
- Using unique_ptr to uniquely own a memory resource
- Using shared_ptr to share a memory resource
- Implementing move semantics
-
Implementing Patterns and Idioms
- Introduction
- Avoiding repetitive if...else statements in factory patterns
- Implementing the pimpl idiom
- Implementing the named parameter idiom
- Separating interfaces and implementations with the non-virtual interface idiom
- Handling friendship with the attorney-client idiom
- Static polymorphism with the curiously recurring template pattern
- Implementing a thread-safe singleton
-
Exploring Testing Frameworks
- Introduction
- Getting started with Boost.Test
- Writing and invoking tests with Boost.Test
- Asserting with Boost.Test
- Using fixtures in Boost.Test
- Controlling outputs with Boost.Test
- Getting started with Google Test
- Writing and invoking tests with Google Test
- Asserting with Google Test
- Using text fixtures with Google Test
- Controlling output with Google Test
- Getting started with Catch
- Writing and invoking tests with Catch
- Asserting with Catch
- Controlling output with Catch
- Bibliography
Product information
- Title: Modern C++ Programming Cookbook
- Author(s):
- Release date: May 2017
- Publisher(s): Packt Publishing
- ISBN: 9781786465184
You might also like
book
Modern C++ Programming Cookbook - Second Edition
A pragmatic recipe book for acquiring a comprehensive understanding of the complexities and core fundamentals of …
book
Embedded Programming with Modern C++ Cookbook
Explore various constraints and challenges that embedded developers encounter in their daily tasks and learn how …
book
C++ System Programming Cookbook
A problem-solution-based guide to help you overcome hurdles effectively while working with kernel APIs, filesystems, networks, …
book
Advanced C++ Programming Cookbook
A recipe-based guide to refining your C++ programming skills with the help of coding best practices, …