logo资料库

C++17 The Complete Guide.pdf

第1页 / 共390页
第2页 / 共390页
第3页 / 共390页
第4页 / 共390页
第5页 / 共390页
第6页 / 共390页
第7页 / 共390页
第8页 / 共390页
资料共390页,剩余部分请下载后查看
C++17 - The Complete Guide
Contents
Preface
About This Book
What You Should Know Before Reading This Book
Overall Structure of the Book
How to Read This Book
The C++17 Standard
Example Code and Additional Information
Feedback
Part I: Basic Language Features
1 Structured Bindings
1.1 Structured Bindings in Detail
1.2 Where Structured Bindings can be Used
1.2.1 Structures and Classes
1.2.2 Raw Arrays
1.2.3 std::pair, std::tuple, and std::array
1.3 Providing a Tuple-Like API for Structured Bindings
1.4 Afternotes
2 if and switch with Initialization
2.1 if with Initialization
2.2 switch with Initialization
2.3 Afternotes
3 Inline Variables
3.1 Motivation of Inline Variables
3.2 Using Inline Variables
3.3 constexpr now implies inline
3.4 Inline Variables and thread_local
3.5 Afternotes
4 Aggregate Extensions
4.1 Motivation for Extended Aggregate Initialization
4.2 Using Extended Aggregate Initialization
4.3 Definition of Aggregates
4.4 Backward Incompatibilities
4.5 Afternotes
5 Mandatory Copy Elision or Passing Unmaterialized Objects
5.1 Motivation for Mandatory Copy Elision for Temporaries
5.2 Benefit of Mandatory Copy Elision for Temporaries
5.3 Clarified Value Categories
5.3.1 Value Categories
5.3.2 Value Categories Since C++17
5.4 Unmaterialized Return Value Passing
5.5 Afternotes
6 Lambda Extensions
6.1 constexpr Lambdas
6.2 Passing Copies of this to Lambdas
6.3 Capturing by Reference
6.4 Afternotes
7 New Attributes and Attribute Features
7.1 Attribute [[nodiscard]]
7.2 Attribute [[maybe_unused]]
7.3 Attribute [[fallthrough]]
7.4 General Attribute Extensions
7.5 Afternotes
8 Other Language Features
8.1 Nested Namespaces
8.2 Defined Expression Evaluation Order
8.3 Relaxed Enum Initialization from Integral Values
8.4 Fixed Direct List Initialization with auto
8.5 Hexadecimal Floating-Point Literals
8.6 UTF-8 Character Literals
8.7 Exception Specifications as Part of the Type
8.8 Single-Argument static_assert
8.9 Preprocessor Condition __has_include
8.10 Afternotes
Part II: Template Features
9 Class Template Argument Deduction
9.1 Usage of Class Template Argument Deduction
9.1.1 Copying by Default
9.1.2 Deducing the Type of Lambdas
9.1.3 No Partial Class Template Argument Deduction
9.1.4 Class Template Argument Deduction Instead of Convenience Functions
9.2 Deduction Guides
9.2.1 Using Deduction Guides to Force Decay
9.2.2 Non-Template Deduction Guides
9.2.3 Deduction Guides versus Constructors
9.2.4 Explicit Deduction Guides
9.2.5 Deduction Guides for Aggregates
9.2.6 Standard Deduction Guides
9.3 Afternotes
10 Compile-Time if
10.1 Motivation for Compile-Time if
10.2 Using Compile-Time if
10.2.1 Caveats for Compile-Time if
10.2.2 Other Compile-Time if Examples
10.3 Compile-Time if with Initialization
10.4 Using Compile-Time if Outside Templates
10.5 Afternotes
11 Fold Expressions
11.1 Motivation for Fold Expressions
11.2 Using Fold Expressions
11.2.1 Dealing with Empty Parameter Packs
11.2.2 Supported Operators
11.2.3 Using Fold Expressions for Types
11.3 Afternotes
12 Dealing with Strings as Template Parameters
12.1 Using Strings in Templates
12.2 Afternotes
13 Placeholder Types like auto as Template Parameters
13.1 Using auto as Template Parameter
13.1.1 Parameterizing Templates for Characters and Strings
13.1.2 Defining Metaprogramming Constants
13.2 Using auto as Variable Template Parameter
13.3 Using decltype(auto) as Template Parameter
13.4 Afternotes
14 Extended Using Declarations
14.1 Using Variadic Using Declarations
14.2 Variadic Using Declarations for Inheriting Constructors
14.3 Afternotes
Part III: New Library Components
15 std::optional<>
15.1 Using std::optional<>
15.1.1 Optional Return Values
15.1.2 Optional Arguments and Data Members
15.2 std::optional<> Types and Operations
15.2.1 std::optional<> Types
15.2.2 std::optional<> Operations
15.3 Special Cases
15.3.1 Optional of Boolean or Raw Pointer Values
15.3.2 Optional of Optional
15.4 Afternotes
16 std::variant<>
16.1 Motivation of std::variant<>
16.2 Using std::variant<>
16.3 std::variant<> Types and Operations
16.3.1 std::variant<> Types
16.3.2 std::variant<> Operations
16.3.3 Visitors
16.3.4 Valueless by Exception
16.4 Polymorphism and Inhomogeneous Collections with std::variant
16.4.1 Geometric Objects with std::variant
16.4.2 Other Inhomogeneous Collections with std::variant
16.4.3 Comparing variant Polymorphism
16.5 Special Cases with std::variant<>
16.5.1 Having Both bool and std::string Alternatives
16.6 Afternotes
17 std::any
17.1 Using std::any
17.2 std::any Types and Operations
17.2.1 Any Types
17.2.2 Any Operations
17.3 Afternotes
18 std::byte
18.1 Using std::byte
18.2 std::byte Types and Operations
18.2.1 std::byte Types
18.2.2 std::byte Operations
18.3 Afternotes
19 String Views
19.1 Differences to std::string
19.2 Using String Views
19.3 Using String Views Similar to Strings
19.3.1 String View Considered Harmful
19.4 String View Types and Operations
19.4.1 Concrete String View Types
19.4.2 String View Operations
19.4.3 String View Support by Other Types
19.5 Using String Views in API's
19.5.1 Using String Views to Initialize Strings
19.5.2 Using String Views instead of Strings
19.6 Afternotes
20 The Filesystem Library
20.1 Basic Examples
20.1.1 Print Attributes of a Passed Filesystem Path
20.1.2 Switch Over Filesystem Types
20.1.3 Create Different Types of Files
20.1.4 Dealing with Filesystems Using Parallel Algorithms
20.2 Principles and Terminology
20.2.1 General Portability Disclaimer
20.2.2 Namespace
20.2.3 Paths
20.2.4 Normalization
20.2.5 Member versus Free-Standing Functions
20.2.6 Error Handling
20.2.7 File Types
20.3 Path Operations
20.3.1 Path Creation
20.3.2 Path Inspection
20.3.3 Path I/O and Conversions
20.3.4 Conversions Between Native and Generic Format
20.3.5 Path Modifications
20.3.6 Path Comparisons
20.3.7 Other Path Operations
20.4 Filesystem Operations
20.4.1 File Attributes
20.4.2 File Status
20.4.3 Permissions
20.4.4 Filesystem Modifications
20.4.5 Symbolic Links and Filesystem-Dependent Path Conversions
20.4.6 Other Filesystem Operations
20.5 Iterating Over Directories
20.5.1 Directory Entries
20.6 Afternotes
Part IV: Library Extensions and Modifications
21 Type Traits Extensions
21.1 Type Traits Suffix _v
21.2 New Type Traits
21.3 std::bool_constant<>
21.4 std::void_t<>
21.5 Afternotes
22 Parallel STL Algorithms
22.1 Using Parallel Algorithms
22.1.1 Using a Parallel for_each()
22.1.2 Using a Parallel sort()
22.2 Execution Policies
22.3 Exception Handling
22.4 Benefit of not using Parallel Algorithms
22.5 Overview of Parallel Algorithms
22.6 New Algorithms for Parallel Processing
22.6.1 reduce()
22.7 Parallel Algorithms in Detail
22.8 Afternotes
23 Substring and Subsequence Searchers
23.1 Using Substring Searchers
23.1.1 Using Searchers with search()
23.1.2 Using Searchers Directly
23.2 Using General Subsequence Searchers
23.3 Using Searcher Predicates
23.4 Afternotes
24 Other Utility Functions and Algorithms
24.1 size(), empty(), and data()
24.1.1 Generic size() Function
24.1.2 Generic empty() Function
24.1.3 Generic data() Function
24.2 as_const()
24.2.1 Capturing by Const Reference
24.3 clamp()
24.4 sample()
24.5 for_each_n()
24.6 Afternotes
25 Container Extensions
25.1 Container-Support of Incomplete Types
25.2 Node Handles
25.3 Afternotes
26 Multi-Threading and Concurrency
26.1 Supplementary Mutexes and Locks
26.1.1 std::scoped_lock
26.1.2 std::shared_mutex
26.2 is_always_lock_free() for Atomics
26.3 Cache-Line Sizes
26.4 Afternotes
Part V: Expert Utilities
27 Polymorphic Memory Resources (PMR)
27.1 Using Standard Memory Resources
27.1.1 Motivating Example
27.1.2 Standard Memory Resources
27.1.3 Standard Memory Resources in Detail
27.2 Defining Custom Memory Resources
27.2.1 Equality of Memory Resources
27.3 Providing Memory Resources Support for Custom Types
27.3.1 Definition of a PMR Type
27.3.2 Usage of a PMR Type
27.3.3 Dealing with the Different Types
27.4 Afternotes
28 new and delete with Over-Aligned Data
28.1 Using new with Alignments
28.1.1 Distinct Dynamic/Heap Memory Arenas
28.1.2 Passing the Alignment with the new Expression
28.2 Implementing operator new() for Aligned Memory
28.2.1 Implementing Aligned Allocation Before C++17
28.2.2 Implementing Type-Specific operator new()
28.3 Implementing Global operator new()
28.3.1 Backward Incompatibilities
28.4 Tracking all ::new Calls
28.5 Afternotes
29 Other Library Improvements for Experts
29.1 Low-Level Conversions between Character Sequences and Numeric Values
29.1.1 Example Usage
29.1.2 Floating-Point Round-Trip Support
29.2 Afternotes
Glossary
B
F
I
P
S
V
Index
A
B
C
D
E
F
G
H
I
J
L
M
N
O
P
R
S
T
U
V
W
X
Josuttis: C++17 2019/02/16 18:57 page i Nicolai M. Josuttis C++17 - The Complete Guide
Josuttis: C++17 2019/02/16 18:57 page ii C++17 - The Complete Guide Nicolai M. Josuttis This book is for sale at http://leanpub.com/cpp17. This version was published on 2019/02/16. This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publish- ing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. © 2019 by Nicolai Josuttis. All rights reserved. This book was typeset by Nicolai M. Josuttis using the LATEX document processing system.
Josuttis: C++17 2019/02/16 18:57 page iii Contents Preface Versions of This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About This Book What You Should Know Before Reading This Book . . . . . . . . . . . . . . . . . . . . . . Overall Structure of the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . How to Read This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The C++17 Standard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Example Code and Additional Information . . . . . . . . . . . . . . . . . . . . . . . . . . . Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part I: Basic Language Features 1 Structured Bindings 1.1 Structured Bindings in Detail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Where Structured Bindings can be Used . . . . . . . . . . . . . . . . . . . . . . . . . . . Structures and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.1 Raw Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.2 1.2.3 std::pair, std::tuple, and std::array . . . . . . . . . . . . . . . . . . . 1.3 Providing a Tuple-Like API for Structured Bindings . . . . . . . . . . . . . . . . . . . . 1.4 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 if and switch with Initialization 2.1 if with Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii xiii xiii xiv xvii xvii xviii xviii xix xix xix 1 3 4 8 8 9 9 11 18 19 19
Josuttis: C++17 2019/02/16 18:57 page iv iv Contents switch with Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 2.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Inline Variables 3.1 Motivation of Inline Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Using Inline Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . constexpr now implies inline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 3.4 Inline Variables and thread_local . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.5 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Aggregate Extensions 4.1 Motivation for Extended Aggregate Initialization . . . . . . . . . . . . . . . . . . . . . . 4.2 Using Extended Aggregate Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Definition of Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4 Backward Incompatibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.5 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Mandatory Copy Elision or Passing Unmaterialized Objects 5.1 Motivation for Mandatory Copy Elision for Temporaries . . . . . . . . . . . . . . . . . 5.2 Benefit of Mandatory Copy Elision for Temporaries . . . . . . . . . . . . . . . . . . . . 5.3 Clarified Value Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.1 Value Categories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.2 Value Categories Since C++17 . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.4 Unmaterialized Return Value Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.5 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Lambda Extensions constexpr Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1 6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Passing Copies of this to Lambdas 6.3 Capturing by Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.4 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 New Attributes and Attribute Features 7.1 Attribute [[nodiscard]] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 21 23 23 25 26 27 29 31 32 32 34 34 35 37 37 39 40 40 42 43 44 45 45 47 49 49 51 51
Josuttis: C++17 2019/02/16 18:57 page v Contents 7.2 Attribute [[maybe_unused]] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3 Attribute [[fallthrough]] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.4 General Attribute Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.5 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Other Language Features 8.1 Nested Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Defined Expression Evaluation Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 Relaxed Enum Initialization from Integral Values . . . . . . . . . . . . . . . . . . . . . 8.4 Fixed Direct List Initialization with auto . . . . . . . . . . . . . . . . . . . . . . . . . . 8.5 Hexadecimal Floating-Point Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.6 UTF-8 Character Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exception Specifications as Part of the Type . . . . . . . . . . . . . . . . . . . . . . . . . 8.7 8.8 Single-Argument static_assert . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.9 Preprocessor Condition __has_include . . . . . . . . . . . . . . . . . . . . . . . . . . 8.10 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part II: Template Features 9 Class Template Argument Deduction Copying by Default 9.1 Usage of Class Template Argument Deduction . . . . . . . . . . . . . . . . . . . . . . . 9.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1.2 Deducing the Type of Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1.3 No Partial Class Template Argument Deduction . . . . . . . . . . . . . . . . . Class Template Argument Deduction Instead of Convenience Functions . . 9.1.4 9.2 Deduction Guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2.1 Using Deduction Guides to Force Decay . . . . . . . . . . . . . . . . . . . . . 9.2.2 Non-Template Deduction Guides . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2.3 Deduction Guides versus Constructors . . . . . . . . . . . . . . . . . . . . . . . 9.2.4 Explicit Deduction Guides . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2.5 Deduction Guides for Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Standard Deduction Guides v 52 53 54 54 57 57 58 61 62 63 64 65 68 69 69 71 73 74 75 76 77 78 80 81 81 82 83 83 84 89
Josuttis: C++17 2019/02/16 18:57 page vi vi Contents 10 Compile-Time if 10.1 Motivation for Compile-Time if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Using Compile-Time if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2.1 Caveats for Compile-Time if . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2.2 Other Compile-Time if Examples . . . . . . . . . . . . . . . . . . . . . . . . . 91 92 94 94 97 10.3 Compile-Time if with Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 10.4 Using Compile-Time if Outside Templates . . . . . . . . . . . . . . . . . . . . . . . . . 100 10.5 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 11 Fold Expressions 103 11.1 Motivation for Fold Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 11.2 Using Fold Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 11.2.1 Dealing with Empty Parameter Packs . . . . . . . . . . . . . . . . . . . . . . . 106 11.2.2 Supported Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 11.2.3 Using Fold Expressions for Types . . . . . . . . . . . . . . . . . . . . . . . . . . 112 11.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 12 Dealing with Strings as Template Parameters 115 12.1 Using Strings in Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 12.2 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 13 Placeholder Types like auto as Template Parameters 117 13.1 Using auto as Template Parameter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 13.1.1 Parameterizing Templates for Characters and Strings . . . . . . . . . . . . . . 118 13.1.2 Defining Metaprogramming Constants . . . . . . . . . . . . . . . . . . . . . . . 119 13.2 Using auto as Variable Template Parameter . . . . . . . . . . . . . . . . . . . . . . . . 120 13.3 Using decltype(auto) as Template Parameter . . . . . . . . . . . . . . . . . . . . . . 122 13.4 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 14 Extended Using Declarations 125 14.1 Using Variadic Using Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 14.2 Variadic Using Declarations for Inheriting Constructors . . . . . . . . . . . . . . . . . 126 14.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Josuttis: C++17 2019/02/16 18:57 page vii Contents Part III: New Library Components vii 129 15 std::optional<> 131 15.1 Using std::optional<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 15.1.1 Optional Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 15.1.2 Optional Arguments and Data Members . . . . . . . . . . . . . . . . . . . . . . 133 15.2 std::optional<> Types and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 135 15.2.1 std::optional<> Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 15.2.2 std::optional<> Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 15.3 Special Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 15.3.1 Optional of Boolean or Raw Pointer Values . . . . . . . . . . . . . . . . . . . . 140 15.3.2 Optional of Optional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 15.4 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 16 std::variant<> 143 16.1 Motivation of std::variant<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 16.2 Using std::variant<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 16.3 std::variant<> Types and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 16.3.1 std::variant<> Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 16.3.2 std::variant<> Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 16.3.3 Visitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 16.3.4 Valueless by Exception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 16.4 Polymorphism and Inhomogeneous Collections with std::variant . . . . . . . . . 156 16.4.1 Geometric Objects with std::variant . . . . . . . . . . . . . . . . . . . . . . 156 16.4.2 Other Inhomogeneous Collections with std::variant . . . . . . . . . . . . 159 16.4.3 Comparing variant Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . 160 16.5 Special Cases with std::variant<> . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 16.5.1 Having Both bool and std::string Alternatives . . . . . . . . . . . . . . . 161 16.6 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 17 std::any 163 17.1 Using std::any . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 17.2 std::any Types and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 17.2.1 Any Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Josuttis: C++17 2019/02/16 18:57 page viii viii Contents 17.2.2 Any Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 17.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 18 std::byte 171 18.1 Using std::byte . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 18.2 std::byte Types and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 18.2.1 std::byte Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 18.2.2 std::byte Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 18.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 19 String Views 19.3.1 String View Considered Harmful 177 19.1 Differences to std::string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 19.2 Using String Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 19.3 Using String Views Similar to Strings . . . . . . . . . . . . . . . . . . . . . . . . . . 180 19.4 String View Types and Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 19.4.1 Concrete String View Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 19.4.2 String View Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 19.4.3 String View Support by Other Types . . . . . . . . . . . . . . . . . . . . . . . . 187 19.5 Using String Views in API’s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 19.5.1 Using String Views to Initialize Strings . . . . . . . . . . . . . . . . . . . . . . 188 19.5.2 Using String Views instead of Strings . . . . . . . . . . . . . . . . . . . . . . . 190 19.6 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 20 The Filesystem Library 193 20.1 Basic Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 20.1.1 Print Attributes of a Passed Filesystem Path . . . . . . . . . . . . . . . . . . . 193 20.1.2 Switch Over Filesystem Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 20.1.3 Create Different Types of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 20.1.4 Dealing with Filesystems Using Parallel Algorithms . . . . . . . . . . . . . . 202 20.2 Principles and Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 20.2.1 General Portability Disclaimer . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 20.2.2 Namespace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 20.2.3 Paths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
分享到:
收藏