logo资料库

c++ 17 the complete guide.pdf

第1页 / 共452页
第2页 / 共452页
第3页 / 共452页
第4页 / 共452页
第5页 / 共452页
第6页 / 共452页
第7页 / 共452页
第8页 / 共452页
资料共452页,剩余部分请下载后查看
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
Error Terminology
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 for Inline Variables
3.2 Using Inline Variables
3.3 constexpr Now Implies inline For Static Members
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.1.1 Using constexpr Lambdas
6.2 Passing Copies of this to Lambdas
6.3 Capturing by const 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 Use 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 String Literals as Template Parameters
12.1 Using Strings in Templates
12.2 Afternotes
13 Placeholder Types like auto as Template Parameters
13.1 Using auto for Template Parameters
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 for 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 Heterogeneous Collections with std::variant
16.4.1 Geometric Objects with std::variant
16.4.2 Other Heterogeneous 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 Compared to std::string
19.2 Using String Views
19.3 Using String Views as Parameters
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 APIs
19.5.1 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 Function 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 Extensions of Type Traits
21.1 Type Traits Suffix _v
21.2 New Type Traits
21.3 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 Motivation for New Algorithms for Parallel Processing
22.6.1 reduce()
22.7 Afternotes
23 New STL Algorithms in Detail
23.1 std::for_each_n()
23.2 New Numeric STL Algorithms
23.2.1 std::reduce()
23.2.2 std::transform_reduce()
23.2.3 std::inclusive_scan() and std::exclusive_scan()
23.2.4 std::transform_inclusive_scan() and std::transform_exclusive_scan()
23.3 Afternotes
24 Substring and Subsequence Searchers
24.1 Using Substring Searchers
24.1.1 Using Searchers with search()
24.1.2 Using Searchers Directly
24.2 Using General Subsequence Searchers
24.3 Using Searcher Predicates
24.4 Afternotes
25 Other Utility Functions and Algorithms
25.1 size(), empty(), and data()
25.1.1 Generic size() Function
25.1.2 Generic empty() Function
25.1.3 Generic data() Function
25.2 as_const()
25.2.1 Capturing by Const Reference
25.3 clamp()
25.4 sample()
25.5 Afternotes
26 Container and String Extensions
26.1 Node Handles
26.1.1 Modifying a Key
26.1.2 Moving Nodes Between Containers
26.1.3 Merging Containers
26.2 Emplace Improvements
26.2.1 Return Type of Emplace Functions
26.2.2 try_emplace() and insert_or_assign() for Maps
26.2.3 try_emplace()
26.2.4 insert_or_assign()
26.3 Container Support for Incomplete Types
26.4 String Improvements
26.5 Afternotes
27 Multi-Threading and Concurrency
27.1 Supplementary Mutexes and Locks
27.1.1 std::scoped_lock
27.1.2 std::shared_mutex
27.2 is_always_lock_free for Atomics
27.3 Cache Line Sizes
27.4 Afternotes
28 Other Small Library Features and Modifications
28.1 std::uncaught_exceptions()
28.2 Shared Pointer Improvements
28.2.1 Special handling for Shared Pointers to Raw C Arrays
28.2.2 reinterpret_pointer_cast for Shared Pointers
28.2.3 weak_type for Shared Pointers
28.2.4 weak_from_this for Shared Pointers
28.3 Numeric Extensions
28.3.1 Greatest Common Divisor and Least Common Multiple
28.3.2 Three-Argument Overloads of std::hypot()
28.3.3 Mathematical Special Functions
28.4 chrono Extensions
28.5 constexpr Extensions and Fixes
28.6 noexcept Extensions and Fixes
28.7 Afternotes
Part V: Expert Utilities
29 Polymorphic Memory Resources (PMR)
29.1 Using Standard Memory Resources
29.1.1 Motivating Example
29.1.2 Standard Memory Resources
29.1.3 Standard Memory Resources in Detail
29.2 Defining Custom Memory Resources
29.2.1 Equality of Memory Resources
29.3 Providing Memory Resource Support for Custom Types
29.3.1 Definition of a PMR Type
29.3.2 Using a PMR Type
29.3.3 Dealing with the Different Types
29.4 Afternotes
30 new and delete with Over-Aligned Data
30.1 Using new with Alignments
30.1.1 Distinct Dynamic/Heap Memory Arenas
30.1.2 Passing the Alignment with the new Expression
30.2 Implementing operator new() for Aligned Memory
30.2.1 Implementing Aligned Allocation Before C++17
30.2.2 Implementing Type-Specific operator new()
30.3 Implementing Global operator new()
30.3.1 Backward Incompatibilities
30.4 Tracking All ::new Calls
30.5 Afternotes
31 std::to_chars() and std::from_chars()
31.1 Motivation for Low-Level Conversions between Character Sequences and Numeric Values
31.2 Example Usage
31.2.1 from_chars()
31.2.2 to_chars()
31.3 Floating-Point Round-Trip Support
31.4 Afternotes
32 std::launder()
32.1 Motivation for std::launder()
32.2 How launder() Solves the Problem
32.3 Why/When launder() Does Not Work
32.4 Afternotes
33 Improvements for Implementing Generic Code
33.1 std::invoke<>()
33.2 std::bool_constant<>
33.3 std::void_t<>
33.4 Afternotes
Part VI: Final General Hints
34 Common C++17 Settings
34.1 Value of __cplusplus
34.2 Compatibility to C11
34.3 Dealing with Signal Handlers
34.4 Forward Progress Guarantees
34.5 Afternotes
35 Deprecated and Removed Features
35.1 Deprecated and Removed Core Language Features
35.1.1 Throw Specifications
35.1.2 Keyword register
35.1.3 Disable ++ for bool
35.1.4 Trigraphs
35.1.5 Definition/Redeclaration of static constexpr Members
35.2 Deprecated and Removed Library Features
35.2.1 auto_ptr
35.2.2 Algorithm random_shuffle()
35.2.3 unary_function and binary_function
35.2.4 ptr_fun(), mem_fun(), and Binders
35.2.5 Allocator Support for std::function<>
35.2.6 Deprecated IOStream Aliases
35.2.7 Deprecated Library Features
35.3 Afternotes
Glossary
B
F
I
P
S
V
Index
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
R
S
T
U
V
W
X
Josuttis: C++17 2019/12/20 17:30 page i Nicolai M. Josuttis C++17 - The Complete Guide First Edition
Josuttis: C++17 2019/12/20 17:30 page ii C++17 - The Complete Guide First Edition Nicolai M. Josuttis This version was published on 2019-12-20. © 2019 by Nicolai Josuttis. All rights reserved. This publication is protected by copyright, and permission must be obtained from the author prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. This book was typeset by Nicolai M. Josuttis using the LATEX document processing system. This book is for sale at http://leanpub.com/cpp17. This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing 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.
Josuttis: C++17 2019/12/20 17:30 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . Providing a Tuple-Like API for Structured Bindings . . . . . . . . . . . . . . . . . . . . . . . . 1.3 1.4 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 if and switch with Initialization 2.1 if with Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii xv xv xvii xix xix xx xx xx xxi xxi xxi 1 3 4 7 8 9 9 11 19 21 21
Josuttis: C++17 2019/12/20 17:30 page iv iv Contents 2.2 switch with Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Inline Variables 3.1 Motivation for Inline Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Using Inline Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . constexpr Now Implies inline For Static Members . . . . . . . . . . . . . . . . . . . . . . 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 6.1 constexpr Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.1.1 Using constexpr Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Passing Copies of this to Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.3 Capturing by const Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.4 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 New Attributes and Attribute Features 7.1 Attribute [[nodiscard]] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 24 25 25 27 28 29 31 33 34 34 36 36 37 39 39 41 42 42 44 45 46 47 47 49 50 53 53 55 55
Josuttis: C++17 2019/12/20 17:30 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 Use 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 57 58 58 59 61 61 62 65 66 67 68 69 72 73 73 75 77 77 79 80 81 83 84 85 86 86 87 88 89 93
Josuttis: C++17 2019/12/20 17:30 page vi vi 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 String Literals as Template Parameters 12.1 Using Strings in Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Placeholder Types like auto as Template Parameters 13.1 Using auto for Template Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Contents 95 96 98 98 101 103 104 105 107 108 108 109 112 117 118 119 119 120 121 121 122 123 124 126 127 129 129 130 132
Josuttis: C++17 2019/12/20 17:30 page vii Contents 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 for 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 Heterogeneous Collections with std::variant . . . . . . . . . . . . . . 16.4.1 Geometric Objects with std::variant . . . . . . . . . . . . . . . . . . . . . . . . . . 16.4.2 Other Heterogeneous 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii 133 135 135 135 137 139 139 139 146 146 146 147 149 149 150 152 152 153 157 161 162 163 165 167 168 168 168 171 171 174 174 174
Josuttis: C++17 2019/12/20 17:30 page viii viii Contents 17.3 Afternotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 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.3.1 String View Considered Harmful 19.1 Differences Compared to std::string . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19.2 Using String Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19.3 Using String Views as Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19.5.1 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 Function versus Free-Standing Functions . . . . . . . . . . . . . . . . . . . . 20.2.6 Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 179 181 181 182 184 185 185 186 186 188 192 192 193 196 196 197 198 201 201 201 204 206 210 210 210 211 211 212 213 214
分享到:
收藏