Programming with C++20: Concepts, Coroutines, Ranges, and more

Programming with C++20: Concepts, Coroutines, Ranges, and more

Programming with C++20: Concepts, Coroutines, Ranges, and more
Автор: Fertig Andreas
Дата выхода: 2024
Издательство: Independent publishing
Количество страниц: 326
Размер файла: 1,5 МБ
Тип файла: PDF
Добавил: it-books
 Дополнительные материалы 

Programming with C20 - Concepts, Coroutines, Ranges, and more....3

Using Code Examples....5

Code download....5

Used Compilers....5

About the Author....6

About the Book....7

Style and conventions....7

Feedback....8

PDFPaperback vs. epub....8

Thank you....9

Revision History....9

1: Concepts: Predicates for strongly typed generic code....11

1.1: Programming before Concepts....12

1.2: Start using Concepts....15

1.3: Application areas for Concepts....16

1.4: The requires-expression: The runway for Concepts....18

1.5: Requirement kinds in a requires-expression....20

1.5.1: The simple requirement....20

1.5.2: The nested requirement....20

1.5.3: The compound requirement....21

1.5.4: The type requirement....22

1.6: Ad hoc constraints....24

1.7: Defining a concept....25

1.8: Testing requirements....27

1.9: Abbreviated function template with auto as a generic parameter....29

1.9.1: What does such a construct do?....29

1.9.2: Exemplary use case: Requiring a parameter type to be an invocable....29

1.10: Using a constexpr function in a concept....31

1.11: Concepts and constrained auto types....33

1.11.1: Constrained auto variables....33

1.11.2: Constrained auto return-type....34

1.12: The power of Concepts: requires instead of enable_if....35

1.12.1: Call method based on requires....35

1.12.2: Conditional copy operations....36

1.12.3: Conditional destructor....38

1.12.4: Conditional methods....39

1.13: Concepts ordering....40

1.13.1: Subsumption rule details....43

1.13.2: One more thing, never say not....46

1.14: Improved error message....48

1.15: Existing Concepts....53

2: Coroutines: Suspending functions....54

2.1: Regular functions and their control flow....55

2.2: What are Coroutines....57

2.2.1: Generating a sequence with coroutines....57

2.3: The Elements of Coroutines in C....59

2.3.1: Stackless Coroutines in C....59

2.3.2: The new kids on the block: co_await, co_return and co_yield....59

2.3.3: The generator....59

2.3.4: The promise_type....61

2.3.5: An iterator for generator....62

2.3.6: Coroutine customization points....63

2.3.7: Coroutines restrictions....65

2.4: Writing a byte-stream parser the old way....67

2.5: A byte-stream parser with Coroutines....70

2.5.1: Writing the Parse function as coroutine....70

2.5.2: Creating an Awaitable type....71

2.5.3: A more flexible promise_type....74

2.5.4: Another generator the FSM....74

2.5.5: Simulating a network byte stream....75

2.5.6: Plugging the pieces together....75

2.6: A different strategy of the Parse generator....78

2.7: Using a coroutine with custom new delete....84

2.8: Using a coroutine with a custom allocator....86

2.9: Exceptions in coroutines....88

Option 1: Let it crash....89

Option 2: Controlled termination....89

Option 3: Re-throw the exception....89

3: Ranges: The next-generation STL....91

3.1: Motivation....92

3.1.1: Avoid code duplication....92

3.1.2: Consistency....93

3.1.3: Safety....95

3.1.4: Composability....96

3.2: The who is who of ranges....99

3.3: A range....100

3.3.1: What is a common_range?....100

3.3.2: A sized_range....101

3.4: A range algorithm....102

3.4.1: Projections for range algorithms....102

3.5: A view into a range....104

3.6: A range adaptor....106

3.6.1: A custom range adaptor....107

3.7: The new ranges namespaces....110

3.8: Ranges Concepts....111

3.9: Views....113

3.10: Creating a custom range....114

3.10.1: Implementing the view....114

3.10.2: A range adaptor for custom_take_view....116

3.10.3: Add the pipe-syntax to custom_take_view....117

3.10.4: A more generalized pipe-syntax implementation....118

4: Modules: The superior way of includes....120

4.1: Background about the need for modules....121

4.1.1: The include hell....121

4.1.2: I like to have secrets....122

4.2: Creating modules....124

4.2.1: A header unit....124

4.2.2: A named module....125

4.3: Applying modules to an existing code base....128

4.3.1: Down with namespace details....128

4.3.2: Now I can hide my secrets from you....129

4.3.3: What you gain....130

4.3.4: Templates in modules....131

4.3.5: Down with DEBUG....131

4.3.6: In-line definitions of class member functions....133

4.3.7: There are some limits....134

5: std::format: Modern & type-safe text formatting....135

5.1: Formatting a string before C20....136

5.1.1: Formatting a stock index with iostreams....138

Localized formatting....141

Formatting with iostreams - a summary....142

5.1.2: Formatting a stock index with printf....142

Formatting with printf - a summary....143

5.2: Formatting a string using std::format....144

5.2.1: std::format specifiers....144

5.2.2: Escaping....146

5.2.3: Localization....147

5.2.4: Formatting floating-point numbers....148

5.3: Formatting a custom type....150

5.3.1: Writing a custom formatter....150

5.3.2: Parsing a custom format specifier....151

5.4: Referring to a format argument....155

5.5: Using a custom buffer....157

5.5.1: Formatting into a dynamically sized buffer....157

5.5.2: Formatting into a fixed sized buffer....158

5.6: Writing our own logging function....160

5.6.1: Prefer make_format_args when forwarding an argument pack....161

5.6.2: Create the format specifier at compile-time....162

5.6.3: Formatting the time....164

6: Three-way comparisons: Simplify your comparisons....166

6.1: Writing a class with equal comparison....168

6.1.1: Comparing different types....169

6.1.2: Less hand-written code with operator reverse, rewrite and default....171

6.2: Writing a class with ordering comparison, pre C20....172

6.3: Writing a class with ordering comparison in C20....175

6.3.1: Member-wise comparison with default....175

6.3.2: Using the STL comparison function....177

6.4: The different comparison categories....179

6.4.1: The comparison categories....179

6.4.2: The comparison strength: strong or weak....180

6.4.3: Another comparison strength: partial ordering....180

6.4.4: Named comparison functions....182

6.5: Converting between comparison categories....184

6.6: New operator abilities: reverse and rewrite....186

6.7: The power of the default spaceship....188

6.8: Applying a custom sort order....191

6.9: Spaceship-operation interaction with existing code....193

7: Lambdas in C20: New features....196

7.1: [, this] as a lambda capture....197

7.2: Default-constructible lambdas....200

7.3: Captureless lambdas in unevaluated contexts....202

7.4: Lambdas in generic code....204

7.4.1: Lambdas with templated-head....205

7.4.2: Variadic lambda arguments....206

7.4.3: Forwarding variadic lambda arguments....207

7.5: Pack expansions in lambda init-captures....209

7.6: Restricting lambdas with Concepts....212

8: Aggregates: Designated initializers and more....216

8.1: What is an aggregate....217

8.2: Designated initializers....219

8.2.1: Designated initializers in C....219

8.2.2: Designated initializers in C20....220

8.2.3: Initializing a subset of an aggregate with designated initializers....223

8.2.4: Initialize a subset with designated initializers without default member initializers....224

Utilizing Return Value Optimization thanks to designated initializers....225

8.2.5: Named arguments in C: Aggregates with designated initializers....226

8.2.6: Overload resolution and designated initializers....228

8.3: Direct-initialization for aggregates....230

8.3.1: Initialization forms: Braced or parenthesis initialization....230

8.3.2: Aggregates with user-declared constructors....233

8.4: Class Template Argument Deduction for aggregates....236

9: Class-types as non-type template parameters....242

9.1: What are non-type template parameters again....243

9.2: The requirements for class types as non-type template parameters....245

9.3: Class types as non-type template parameters....247

9.3.1: A first contact with class types as NTTP....247

9.3.2: What compile-time data do we have....249

9.4: Building a format function with specifier count check....250

9.4.1: A first print function....251

9.4.2: Optimizing the format string creation....252

9.4.3: Checking the number of specifiers in a format string....253

9.4.4: Checking if type and specifiers do match....254

9.4.5: Enable more use-cases and prevent mistakes....257

10: New STL elements....260

10.1: bit_cast: Reinterpreting your objects....261

10.2: endian: Endianness detection at compile time....264

10.3: to_array....267

10.4: span: A view of continuous memory....269

10.5: source_location: The modern way for __FUNCTION__....273

10.5.1: Writing a custom assert function....273

10.5.2: Writing a custom assert function with C20....275

10.5.3: Writing a custom log function with C20....276

10.6: contains for all associative containers....280

10.7: starts_with and ends_with for std::string....282

11: Language Updates....284

11.1: Range-based for-loops with initializers....285

11.1.1: Using a counter-variable in a range-based for-loop....285

11.1.2: A workaround for temporaries....287

11.2: New Attributes....289

11.2.1: likely unlikely....289

11.2.2: no_unique_address....290

11.3: using enums....293

11.4: conditional explicit....295

11.4.1: Writing a well-behaved wrapper....295

11.4.2: Communicate your intention, explicitly....296

12: Doing (more) things at compile-time....298

12.1: The two worlds: compile- vs. run-time....299

12.1.1: The benefit of compile-time execution....302

12.2: is_constant_evaluated: Is this a constexpr-context?....304

12.2.1: Different things at compile- and run-time....305

12.2.2: is_constant_evaluated is a run-time value....307

12.3: Less restrictive constexpr-function requirements....308

12.3.1: new delete: Dynamic allocations during compile-time....308

12.3.2: A constexpr std::vector....309

12.4: Utilizing the new compile-time world: Sketching a car racing game....310

12.5: consteval: Do things guaranteed at compile-time....314

12.5.1: as_constant a use-case for consteval....315

12.5.2: Force compile-time evaluation for compile-time checks....316

12.5.3: is_constant_evaluated doesnt make it compile-time....318

12.6: constinit: Initialize a non-const object at compile-time....320

12.6.1: The static initialization order problem....320

12.6.2: Ensure compile-time initialization....322

Acronyms....324

Bibliography....326

Programming with C++20 teaches programmers with C++ experience the new features of C++20 and how to apply them. It does so by assuming C++11 knowledge. Elements of the standards between C++11 and C++20 will be briefly introduced, if necessary. However, the focus is on teaching the features of C++20.

You will start with learning about the so-called big four Concepts, Coroutines, std::ranges, and modules. The big four a followed by smaller yet not less important features. You will learn about std::format, the new way to format a string in C++. In chapter 6, you will learn about a new operator, the so-called spaceship operator, which makes you write less code.

You then will look at various improvements of the language, ensuring more consistency and reducing surprises. You will learn how lambdas improved in C++20 and what new elements you can now pass as non-type template parameters. Your next stop is the improvements to the STL.

Of course, you will not end this book without learning about what happened in the constexpr-world.


Похожее:

Список отзывов:

Нет отзывов к книге.