Mastering the C++17 STL: Make full use of the standard library components in C++17

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Key Features

  • Boost your productivity as a C++ developer with the latest features of C++17
  • Develop high-quality, fast, and portable applications with the varied features of the STL
  • Migrate from older versions (C++11, C++14) to C++17

Book Description

Modern C++ has come a long way since 2011. The latest update, C++17, has just been ratified and several implementations are on the way.

This book is your guide to the C++ standard library, including the very latest C++17 features.

The book starts by exploring the C++ Standard Template Library in depth. You will learn the key differences between classical polymorphism and generic programming, the foundation of the STL. You will also learn how to use the various algorithms and containers in the STL to suit your programming needs. The next module delves into the tools of modern C++. Here you will learn about algebraic types such as std::optional, vocabulary types such as std::function, smart pointers, and synchronization primitives such as std::atomic and std::mutex. In the final module, you will learn about C++'s support for regular expressions and file I/O.

By the end of the book you will be proficient in using the C++17 standard library to implement real programs, and you'll have gained a solid understanding of the library's own internals.

What you will learn

  • Make your own iterator types, allocators, and thread pools.
  • Master every standard container and every standard algorithm.
  • Improve your code by replacing new/delete with smart pointers.
  • Understand the difference between monomorphic algorithms, polymorphic algorithms, and generic algorithms.
  • Learn the meaning and applications of vocabulary type, product type and sum type.

About the Author

Arthur O'Dwyer has used modern C++ in his day job for about ten years--since the days when "modern C++" meant "classic C++." Between 2006 and 2011 he worked on the Green Hills C++ compiler. Since 2014 he has organized a weekly C++ meetup in the San Francisco Bay Area, and he speaks regularly on topics such as those to be found in this book. Later this year, he will attend an ISO C++ committee meeting for the second time.

This is his first book.

Table of Contents

  1. Classical Polymorphism versus Generic Programming
  2. Iterators and Ranges
  3. The Iterator-Pair Algorithms
  4. The Container Zoo
  5. Vocabulary Types
  6. Smart Pointers
  7. Concurrency
  8. Allocators
  9. Iostreams
  10. Regular Expressions
  11. Random numbers
  12. Filesystem

Author(s): Arthur O'Dwyer
Publisher: Packt Publishing
Year: 2017

Language: English
Commentary: True PDF
Pages: 384

Cover
Copyright
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Table of Contents
Preface
Chapter 1: Classical Polymorphism and Generic Programming
Concrete monomorphic functions
Classically polymorphic functions
Generic programming with templates
Summary
Chapter 2: Iterators and Ranges
The problem with integer indices
On beyond pointers
Const iterators
A pair of iterators defines a range
Iterator categories
Input and output iterators
Putting it all together
The deprecated std::iterator
Summary
Chapter 3: The Iterator-Pair Algorithms
A note about headers
Read-only range algorithms
Shunting data with std::copy
Variations on a theme - std::move and std::move_iterator
Complicated copying with std::transform
Write-only range algorithms
Algorithms that affect object lifetime
Our first permutative algorithm: std::sort
Swapping, reversing, and partitioning
Rotation and permutation
Heaps and heapsort
Merges and mergesort
Searching and inserting in a sorted array with std::lower_bound
Deleting from a sorted array with std::remove_if
Summary
Chapter 4: The Container Zoo
The notion of ownership
The simplest container: std::array<T, N>
The workhorse: std::vector<T>
Resizing a std::vector
Inserting and erasing in a std::vector
Pitfalls with vector<bool>
Pitfalls with non-noexcept move constructors
The speedy hybrid: std::deque<T>
A particular set of skills: std::list<T>
What are the special skills of std::list?
Roughing it with std::forward_list<T>
Abstracting with std::stack<T> and std::queue<T>
The useful adaptor: std::priority_queue<T>
The trees: std::set<T> and std::map<K, V>
A note about transparent comparators
Oddballs: std::multiset<T> and std::multimap<K, V>
The hashes: std::unordered_set<T> and std::unordered_map<K, V>
Load factor and bucket lists
Where does the memory come from?
Summary
Chapter 5: Vocabulary Types
The story of std::string
Tagging reference types with reference_wrapper
C++11 and algebraic types
Working with std::tuple
Manipulating tuple values
A note about named classes
Expressing alternatives with std::variant
Visiting variants
What about make_variant? and a note on value semantics
Delaying initialization with std::optional
Revisiting variant
Infinite alternatives with std::any
std::any versus polymorphic class types
Type erasure in a nutshell
std::any and copyability
Again with the type erasure: std::function
std::function, copyability, and allocation
Summary
Chapter 6: Smart Pointers
The origins of smart pointers
Smart pointers never forget
Automatically managing memory with std::unique_ptr<T>
Why C++ doesn't have the finally keyword
Customizing the deletion callback
Managing arrays with std::unique_ptr<T[]>
Reference counting with std::shared_ptr<T>
Don't double-manage!
Holding nullable handles with weak_ptr
Talking about oneself with std::enable_shared_from_this
The Curiously Recurring Template Pattern
A final warning
Denoting un-special-ness with observer_ptr<T>
Summary
Chapter 7: Concurrency
The problem with volatile
Using std::atomic<T> for thread-safe accesses
Doing complicated operations atomically
Big atomics
Taking turns with std::mutex
"Taking locks" the right way
Always associate a mutex with its controlled data
Special-purpose mutex types
Upgrading a read-write lock
Downgrading a read-write lock
Waiting for a condition
Promises about futures
Packaging up tasks for later
The future of futures
Speaking of threads...
Identifying individual threads and the current thread
Thread exhaustion and std::async
Building your own thread pool
Improving our thread pool's performance
Summary
Chapter 8: Allocators
An allocator is a handle to a memory resource
Refresher - Interfaces versus concepts
Defining a heap with memory_resource
Using the standard memory resources
Allocating from a pool resource
The 500 hats of the standard allocator
Carrying metadata with fancy pointers
Sticking a container to a single memory resource
Using the standard allocator types
Setting the default memory resource
Making a container allocator-aware
Propagating downwards with scoped_allocator_adaptor
Propagating different allocators
Summary
Chapter 9: Iostreams
The trouble with I/O in C++
Buffering versus formatting
Using the POSIX API
Using the standard C API
Buffering in the standard C API
Formatting with printf and snprintf
The classical iostreams hierarchy
Streaming and manipulators
Streaming and wrappers
Solving the sticky-manipulator problem
Formatting with ostringstream
A note on locales
Converting numbers to strings
Converting strings to numbers
Reading a line or word at a time
Summary
Chapter 10: Regular Expressions
What are regular expressions?
A note on backslash-escaping
Reifying regular expressions into std::regex objects
Matching and searching
Pulling submatches out of a match
Converting submatches to data values
Iterating over multiple matches
Using regular expressions for string replacement
A primer on the ECMAScript regex grammar
Non-consuming constructs
Obscure ECMAScript features and pitfalls
Summary
Chapter 11: Random Numbers
Random numbers versus pseudo-random numbers
The problem with rand()
Solving problems with <random>
Dealing with generators
Truly random bits with std::random_device
Pseudo-random bits with std::mt19937
Filtering generator outputs with adaptors
Dealing with distributions
Rolling dice with uniform_int_distribution
Generating populations with normal_distribution
Making weighted choices with discrete_distribution
Shuffling cards with std::shuffle
Summary
Chapter 12: Filesystem
A note about namespaces
A very long note on error-reporting
Using <system_error>
Error codes and error conditions
Throwing errors with std::system_error
Filesystems and paths
Representing paths in C++
Operations on paths
Statting files with directory_entry
Walking directories with directory_iterator
Recursive directory walking
Modifying the filesystem
Reporting disk usage
Summary
Index