CONTENTS IN DETAIL

FOREWORD by Peter Bindels

ACKNOWLEDGMENTS

INTRODUCTION

About This Book

Who Should Read This Book?

What’s in This Book?

Part I: The C++ Core Language

Part II: C++ Libraries and Frameworks

AN OVERTURE TO C PROGRAMMERS

Upgrading to Super C

Function Overloading

References

auto Initialization

Namespaces and Implicit typedef of struct, union, and enum

Intermingling C and C++ Object Files

C++ Themes

Expressing Ideas Concisely and Reusing Code

The C++ Standard Library

Lambdas

Generic Programming with Templates

Class Invariants and Resource Management

Move Semantics

Relax and Enjoy Your Shoes

PART I: THE C++ CORE LANGUAGE

1
UP AND RUNNING

The Structure of a Basic C++ Program

Creating Your First C++ Source File

Main: A C++ Program’s Starting Point

Libraries: Pulling in External Code

The Compiler Tool Chain

Setting Up Your Development Environment

Windows 10 and Later: Visual Studio

macOS: Xcode

Linux and GCC

Text Editors

Bootstrapping C++

The C++ Type System

Declaring Variables

Initializing a Variable’s State

Conditional Statements

Functions

printf Format Specifiers

Revisiting step_function

Comments

Debugging

Visual Studio

Xcode

GCC and Clang Debugging with GDB and LLDB

Summary

2
TYPES

Fundamental Types

Integer Types

Floating-Point Types

Character Types

Boolean Types

The std::byte Type

The size_t Type

void

Arrays

Array Initialization

Accessing Array Elements

A Nickel Tour of for Loops

C-Style Strings

User-Defined Types

Enumeration Types

Plain-Old-Data Classes

Unions

Fully Featured C++ Classes

Methods

Access Controls

Constructors

Initialization

The Destructor

Summary

3
REFERENCE TYPES

Pointers

Addressing Variables

Dereferencing Pointers

The Member-of-Pointer Operator

Pointers and Arrays

Pointers Are Dangerous

void Pointers and std::byte Pointers

nullptr and Boolean Expressions

References

Usage of Pointers and References

Forward-Linked Lists: The Canonical Pointer-Based Data Structure

Employing References

this Pointers

const Correctness

const Member Variables

Member Initializer Lists

auto Type Deduction

Initialization with auto

auto and Reference Types

auto and Code Refactorings

Summary

4
THE OBJECT LIFE CYCLE

An Object’s Storage Duration

Allocation, Deallocation, and Lifetime

Memory Management

Automatic Storage Duration

Static Storage Duration

Thread-Local Storage Duration

Dynamic Storage Duration

Tracing the Object Life Cycle

Exceptions

The throw Keyword

Using try-catch Blocks

stdlib Exception Classes

Handling Exceptions

User-Defined Exceptions

The noexcept Keyword

Call Stacks and Exceptions

A SimpleString Class

Appending and Printing

Using SimpleString

Composing a SimpleString

Call Stack Unwinding

Exceptions and Performance

Alternatives to Exceptions

Copy Semantics

Copy Constructors

Copy Assignment

Default Copy

Copy Guidelines

Move Semantics

Copying Can Be Wasteful

Value Categories

lvalue and rvalue References

The std::move Function

Move Construction

Move Assignment

The Final Product

Compiler-Generated Methods

Summary

5
RUNTIME POLYMORPHISM

Polymorphism

A Motivating Example

Adding New Loggers

Interfaces

Object Composition and Implementation Inheritance

Defining Interfaces

Base Class Inheritance

Member Inheritance

virtual Methods

Pure-Virtual Classes and Virtual Destructors

Implementing Interfaces

Using Interfaces

Updating the Bank Logger

Constructor Injection

Property Injection

Choosing Constructor or Property Injection

Summary

6
COMPILE-TIME POLYMORPHISM

Templates

Declaring Templates

Template Class Definitions

Template Function Definitions

Instantiating Templates

Named Conversion Functions

const_cast

static_cast

reinterpret_cast

narrow_cast

mean: A Template Function Example

Genericizing mean

Template Type Deduction

SimpleUniquePointer: A Template Class Example

Type Checking in Templates

Concepts

Defining a Concept

Type Traits

Requirements

Building Concepts from Requires Expressions

Using Concepts

Ad Hoc Requires Expressions

static_assert: The Preconcepts Stopgap

Non-Type Template Parameters

Variadic Templates

Advanced Template Topics

Template Specialization

Name Binding

Type Function

Template Metaprogramming

Template Source Code Organization

Polymorphism at Runtime vs. Compile Time

Summary

7
EXPRESSIONS

Operators

Logical Operators

Arithmetic Operators

Assignment Operators

Increment and Decrement Operators

Comparison Operators

Member Access Operators

Ternary Conditional Operator

The Comma Operator

Operator Overloading

Overloading Operator new

Operator Precedence and Associativity

Evaluation Order

User-Defined Literals

Type Conversions

Implicit Type Conversions

Explicit Type Conversion

C-Style Casts

User-Defined Type Conversions

Constant Expressions

A Colorful Example

The Case for constexpr

Volatile Expressions

Summary

8
STATEMENTS

Expression Statements

Compound Statements

Declaration Statements

Functions

Namespaces

Type Aliasing

Structured Bindings

Attributes

Selection Statements

if Statements

switch Statements

Iteration Statements

while Loops

do-while Loops

for Loops

Ranged-Based for Loops

Jump Statements

break Statements

continue Statements

goto Statements

Summary

9
FUNCTIONS

Function Declarations

Prefix Modifiers

Suffix Modifiers

auto Return Types

auto and Function Templates

Overload Resolution

Variadic Functions

Variadic Templates

Programming with Parameter Packs

Revisiting the sum Function

Fold Expressions

Function Pointers

Declaring a Function Pointer

Type Aliases and Function Pointers

The Function-Call Operator

A Counting Example

Lambda Expressions

Usage

Lambda Parameters and Bodies

Default Arguments

Generic Lambdas

Lambda Return Types

Lambda Captures

constexpr Lambda Expressions

std::function

Declaring a Function

An Extended Example

The main Function and the Command Line

The Three main Overloads

Exploring Program Parameters

A More Involved Example

Exit Status

Summary

PART II: C++ LIBRARIES AND FRAMEWORKS

10
TESTING

Unit Tests

Integration Tests

Acceptance Tests

Performance Tests

An Extended Example: Taking a Brake

Implementing AutoBrake

Test-Driven Development

Adding a Service-Bus Interface

Unit-Testing and Mocking Frameworks

The Catch Unit-Testing Framework

Google Test

Boost Test

Summary: Testing Frameworks

Mocking Frameworks

Google Mock

HippoMocks

A Note on Other Mocking Options: FakeIt and Trompeloeil

Summary

11
SMART POINTERS

Smart Pointers

Smart Pointer Ownership

Scoped Pointers

Constructing

Bring in the Oath Breakers

Implicit bool Conversion Based on Ownership

RAII Wrapper

Pointer Semantics

Comparison with nullptr

Swapping

Resetting and Replacing a scoped_ptr

Non-transferability

boost::scoped_array

A Partial List of Supported Operations

Unique Pointers

Constructing

Supported Operations

Transferable, Exclusive Ownership

Unique Arrays

Deleters

Custom Deleters and System Programming

A Partial List of Supported Operations

Shared Pointers

Constructing

Specifying an Allocator

Supported Operations

Transferable, Non-Exclusive Ownership

Shared Arrays

Deleters

A Partial List of Supported Operations

Weak Pointers

Constructing

Obtaining Temporary Ownership

Advanced Patterns

Supported Operations

Intrusive Pointers

Summary of Smart Pointer Options

Allocators

Summary

12
UTILITIES

Data Structures

tribool

optional

pair

tuple

any

variant

Date and Time

Boost DateTime

Chrono

Numerics

Numeric Functions

Complex Numbers

Mathematical Constants

Random Numbers

Numeric Limits

Boost Numeric Conversion

Compile-Time Rational Arithmetic

Summary

13
CONTAINERS

Sequence Containers

Arrays

Vectors

Niche Sequential Containers

Associative Containers

Sets

Unordered Sets

Maps

Niche Associative Containers

Graphs and Property Trees

The Boost Graph Library

Boost Property Trees

Initializer Lists

Summary

14
ITERATORS

Iterator Categories

Output Iterators

Input Iterators

Forward Iterators

Bidirectional Iterators

Random-Access Iterators

Contiguous Iterators

Mutable Iterators

Auxiliary Iterator Functions

std::advance

std::next and std::prev

std::distance

std::iter_swap

Additional Iterator Adapters

Move Iterator Adapters

Reverse Iterator Adapters

Summary

15
STRINGS

std::string

Constructing

String Storage and Small String Optimizations

Element and Iterator Access

String Comparisons

Manipulating Elements

Search

Numeric Conversions

String View

Constructing

Supported string_view Operations

Ownership, Usage, and Efficiency

Regular Expressions

Patterns

basic_regex

Algorithms

Boost String Algorithms

Boost Range

Predicates

Classifiers

Finders

Modifying Algorithms

Splitting and Joining

Searching

Boost Tokenizer

Localizations

Summary

16
STREAMS

Streams

Stream Classes

Stream State

Buffering and Flushing

Manipulators

User-Defined Types

String Streams

File Streams

Stream Buffers

Random Access

Summary

17
FILESYSTEMS

Filesystem Concepts

std::filesystem::path

Constructing Paths

Decomposing Paths

Modifying Paths

Summary of Filesystem Path Methods

Files and Directories

Error Handling

Path-Composing Functions

Inspecting File Types

Inspecting Files and Directories

Manipulating Files and Directories

Directory Iterators

Constructing

Directory Entries

Recursive Directory Iteration

fstream Interoperation

Summary

18
ALGORITHMS

Algorithmic Complexity

Execution Policies

Non-Modifying Sequence Operations

all_of

any_of

none_of

for_each

for_each_n

find, find_if, and find_if_not

find_end

find_first

adjacent_find

count

mismatch

equal

is_permutation

search

search_n

Mutating Sequence Operations

copy

copy_n

copy_backward

move

move_backward

swap_ranges

transform

replace

fill

generate

remove

unique

reverse

sample

shuffle

Sorting and Related Operations

sort

stable_sort

partial_sort

is_sorted

nth_element

Binary Search

lower_bound

upper_bound

equal_range

binary_search

Partitioning Algorithms

is_partitioned

partition

partition_copy

stable_partition

Merging Algorithms

merge

Extreme-Value Algorithms

min and max

min_element and max_element

clamp

Numeric Operations

Useful Operators

iota

accumulate

reduce

inner_product

adjacent_difference

partial_sum

Other Algorithms

Boost Algorithm

19
CONCURRENCY AND PARALLELISM

Concurrent Programming

Asynchronous Tasks

Sharing and Coordinating

Low-Level Concurrency Facilities

Parallel Algorithms

An Example: Parallel sort

Parallel Algorithms Are Not Magic

Summary

20
NETWORK PROGRAMMING WITH BOOST ASIO

The Boost Asio Programming Model

Network Programming with Asio

The Internet Protocol Suite

Hostname Resolution

Connecting

Buffers

Reading and Writing Data with Buffers

The Hypertext Transfer Protocol (HTTP)

Implementing a Simple Boost Asio HTTP Client

Asynchronous Reading and Writing

Serving

Multithreading Boost Asio

Summary

21
WRITING APPLICATIONS

Program Support

Handling Program Termination and Cleanup

Communicating with the Environment

Managing Operating System Signals

Boost ProgramOptions

The Options Description

Parsing Options

Storing and Accessing Options

Putting It All Together

Special Topics in Compilation

Revisiting the Preprocessor

Compiler Optimization

Linking with C

Summary

INDEX

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset