Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
josuttis_nm_c20_the_complete_guide.pdf
Скачиваний:
44
Добавлен:
27.03.2023
Размер:
5.85 Mб
Скачать

Preface

C++20 is the next evolution in modern C++ programming, and it is already (partially) supported by the latest version of GCC, Clang, and Visual C++. The move to C++20 is at least as big a step as the move to C++11. C++20 contains a significant number of new language features and libraries that will again change the way we program in C++. This applies to both application programmers and programmers who provide foundation libraries.

An Experiment

This book is an experiment in two ways:

I am writing an in-depth book that covers complex new features invented and provided by different programmers and C++ working groups. However, I can ask questions and I do.

I am publishing the book myself on Leanpub and for printing on demand. That is, this book is written step by step and I will publish new versions as soon there is a significant improvement that makes the publication of a new version worthwhile.

The good thing is:

You get the view of the language features from an experienced application programmer—somebody who feels the pain a feature might cause and asks the relevant questions to be able to explain the motivation for a feature, its design, and all consequences for using it in practice.

You can benefit from my experience with C++20 while I am still learning and writing.

This book and all readers can benefit from your early feedback.

This means that you are also part of the experiment. So help me out: give feedback about flaws, errors, features that are not explained well, or gaps, so that we all can benefit from these improvements.

Acknowledgments

This book would not have been possible without the help and support of a huge number of people.

First of all, I would like to thank you, the C++ community, for making this book possible. The incredible design of all the features of C++20, the helpful feedback, and your curiosity are the basis for the evolution of a successful language. In particular, thanks for all the issues you told me about and explained and for the feedback you gave.

xix

xx

Preface

I would especially like to thank everyone who reviewed drafts of this book or corresponding slides and provided valuable feedback and clarification. These reviews increased the quality of the book significantly, again proving that good things are usually the result of collaboration between many people. Therefore, so far (this list is still growing) huge thanks to Carlos Buchart, Javier Estrada, Howard Hinnant, Yung-Hsiang Huang, Daniel Krugler,¨ Dietmar Kuhl,¨ Jens Maurer, Paul Ranson, Thomas Symalla, Steve Vinoski, Ville Voutilainen. Andreas Weis, Hui Xie, Leor Zolman, and Victor Zverovich.

In addition, I would like to thank everyone on the C++ standards committee. In addition to all the work involved in adding new language and library features, these experts spent many, many hours explaining and discussing their work with me, and they did so with patience and enthusiasm. Special thanks here go to Howard Hinnant, Tom Honermann, Tomasz Kaminski, Peter Sommerlad, Tim Song, Barry Revzin, Ville Voutilainen, and Jonathan Wakely.

Special thanks go to the LaTeX community for a great text system and to Frank Mittelbach for solving my LATEX issues (it was almost always my fault).

And finally, many thanks go to my proofreader, Tracey Duffy, who has again done a tremendous job of converting my “German English” into native English.

Versions of This Book

Because this book is written incrementally, the following is a history of the major updates (newest first):

2022-10-29: Describe missing details about floating-point and struct values as NTTPs.

2022-10-29: Describe missing hours utilities.

2022-10-28: Layout all pages.

2022-10-15: Describe missing details of the join view.

2022-10-06: Describe missing issues with standard views and list them all together.

2022-09-28: Describe more range/iterator utility details (move sentinels, iter_move(), iter_swap()).

2022-09-22: Update span documentation and document span operations in detail.

2022-09-16: Fix the utilities for layout compatibility.

2022-08-22: Add complete example programs and fix view details for each view.

2022-08-21: Describe details of the istream view that were added to C++20 as a late fix.

2022-08-17: Describe the new result type for range algorithms.

2022-08-05: Describe how to deal with a non-existing timezone database.

2022-07-29: Improve the description of const issues with views.

2022-07-27: Describe the effect of caching views in detail.

2022-07-21: Describe new iterator categories and iterator_concept.

2022-07-06: Describe concepts for non-type template parameters.

2022-06-28: Describe using requires for member functions to change APIs.

2022-06-19: Describe the fix to the formatting library for compile-time parsing of format strings.

2022-05-31: Split coroutine description into two chapters.

2022-05-24: Describe properties and details of all views.

2022-05-23: Describe coroutine exception handling.

2022-05-24: Describe nested coroutines and symmetric transfer.

2022-05-19: Describe asynchronous communication and thread pools for coroutines.

Versions of This Book

xxi

2022-05-07: Describe jthread and stop token support for condition variables.

2022-04-28: Describe owning views, which were added to C++20 as a late fix.

2022-04-24: Describe significant details of coroutines and their awaiters.

2022-04-21: Describe functions for safe integral comparisons.

2022-04-21: Describe std::ranges::swap().

2022-04-20: Describe the new shift algorithms and the new min/max algorithms for ranges.

2022-04-20: Describe compare_three_way and lexicographical_compare_three_way().

2022-04-19: Describe feature test macros.

2022-04-18: Describe several additional details and clarifications about modules.

2022-04-09: Describe deferred evaluation with auto parameters.

2022-03-26: Describe awaiters, memory management, and other details of coroutines.

2022-03-08: Describe details of utilities of the ranges library.

2022-03-03: Rewrite the coroutines chapter with several more details.

2022-02-20: Describe the range-based for loop with initialization.

2022-02-17: Reorganize book chapters.

2022-02-16: Describe new attributes and attribute extensions.

2022-02-15: Describe conditional explicit.

2022-02-15: Fix the way user-defined formatters should be implemented.

2022-02-12: Describe using for enumeration values.

2022-02-09: Add figure with subset of the subsumptions of the standard concepts.

2022-01-08: Describe class template argument deduction for aggregates.

2022-01-05: Describe modified definition of aggregates.

2022-01-04: Describe aggregate initialization with parentheses.

2021-12-31: Describe the new iterator traits (such as std::iter_value_t).

2021-12-31: Describe where typename is no longer required for type members of template parameters.

2021-12-30: Describe new bit operations (including bit_cast<>()).

2021-12-29: Describe improvements for string types (including std::u8string and using strings at compile time).

2021-12-28: Describe the unseq execution policy for algorithms.

2021-12-25: Describe all other missing lambda features.

2021-12-11: Describe consteval lambdas.

2021-12-06: Describe std::endian.

2021-12-06: Describe synchronized output streams.

2021-12-04: Describe the header file <version>.

2021-11-21: Describe compile-time use of vectors and constexpr extensions.

2021-10-25: Describe designated initializers.

2021-10-14: Describe severe compatibility issues with operator==.

2021-10-14: Describe mathematical constants.

2021-10-12: Describe constinit, consteval, and std::is_constant_evaluated().

2021-10-07: Describe char8_t for UTF-8 (and its compatibility issues).

2021-10-02: Clarify which const views you cannot iterate over.

2021-10-01: Provide details about the formatting library.

2021-09-21: Add updates and fixes according to several reviews.

2021-09-20: Provide a first description of features for modules.

xxii

Preface

2021-09-11: Discuss const issues of ranges and views.

2021-08-30: All concepts are documented.

2021-08-28: Document all new type traits.

2021-08-27: Document iterator utility functions for ranges.

2021-08-26: Document lazy_split_view<>, which was added to C++20 as a late fix.

2021-08-25: Document all missing views of C++20 and all_t<>.

2021-08-20: Document all new iterator and sentinel types.

2021-08-20: Document all primary range adaptors (counted(), all(), and common()).

2021-08-19: Document common_view.

2021-08-16: Document how to deal with semantic constraints of concepts.

2021-08-15: Document type_identity.

2021-07-31: Document empty_view, ref_view, and view_interface.

2021-07-29: Generic code for ranges has to use universal/forwarding references.

2021-07-28: Document iota_view and unreached_sentinel.

2021-07-27: Extend and reorganize chapters about ranges and views.

2021-07-17: Document subrange.

2021-07-09: Document std::source_location.

2021-06-29: The initial published version of the book. The following features are more or less completely described or provide at least a full conceptual introduction:

Comparisons and the spaceship operator <=>

Generic functions

Concepts and requirements (details open)

Ranges and views (details open)

Spans

Non-type template parameter extensions

Formatted output (overview only)

Dates and timezones for <chrono>

Coroutines (first examples)

std::thread and stop tokens

New concurrency features