Thriving in a Crowded and Changing World: C++ 2006–2020
Total Page:16
File Type:pdf, Size:1020Kb
Thriving in a Crowded and Changing World: C++ 2006–2020 BJARNE STROUSTRUP, Morgan Stanley and Columbia University, USA Shepherd: Yannis Smaragdakis, University of Athens, Greece By 2006, C++ had been in widespread industrial use for 20 years. It contained parts that had survived unchanged since introduced into C in the early 1970s as well as features that were novel in the early 2000s. From 2006 to 2020, the C++ developer community grew from about 3 million to about 4.5 million. It was a period where new programming models emerged, hardware architectures evolved, new application domains gained massive importance, and quite a few well-financed and professionally marketed languages fought for dominance. How did C++ ś an older language without serious commercial backing ś manage to thrive in the face of all that? This paper focuses on the major changes to the ISO C++ standard for the 2011, 2014, 2017, and 2020 revisions. The standard library is about 3/4 of the C++20 standard, but this paper’s primary focus is on language features and the programming techniques they support. The paper contains long lists of features documenting the growth of C++. Significant technical points are discussed and illustrated with short code fragments. In addition, it presents some failed proposals and the discussions that led to their failure. It offers a perspective on the bewildering flow of facts and features across the years. The emphasis is on the ideas, people, and processes that shaped the language. Themes include efforts to preserve the essence of C++ through evolutionary changes, to simplify itsuse,to improve support for generic programming, to better support compile-time programming, to extend support for concurrency and parallel programming, and to maintain stable support for decades’ old code. The ISO C++ standard evolves through a consensus process. Inevitably, there is competition among proposals and clashes (usually polite ones) over direction, design philosophies, and principles. The committee is now larger and more active than ever, with as many as 250 people turning up to week-long meetings three times a year and many more taking part electronically. We try (not always successfully) to mitigate the effects of design by committee, bureaucratic paralysis, and excessive enthusiasm for a variety of language fashions. Specific language-technical topics include the memory model, concurrency and parallelism, compile-time computation, move-semantics, exceptions, lambda expressions, and modules. Designing a mechanism for specifying a template’s requirements on its arguments that is sufficiently flexible and precise yet doesn’t impose run-time costs turned out to be hard. The repeated attempts to design łconceptsž to do that have their roots back in the 1980s and touch upon many key design issues for C++ and for generic programming. The description is based on personal participation in the key events and design decisions, backed by the thousands of papers and hundreds of meeting minutes in the ISO C++ standards committee’s archives. CCS Concepts: · Software and its engineering → Software notations and tools; General programming languages; Additional Key Words and Phrases: C++, programming language design and evolution, standardization, generic 70 programming, resource management, concurrency and parallelism, simplification of language use ACM Reference Format: Bjarne Stroustrup. 2020. Thriving in a Crowded and Changing World: C++ 2006ś2020. Proc. ACM Program. Lang. 4, HOPL, Article 70 (June 2020), 167 pages. https://doi.org/10.1145/3386320 Author’s address: Bjarne Stroustrup, Morgan Stanley and Columbia University, USA, [email protected]. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights for third-party components of this work must be honored. For all otheruses, Thiscontact work the is owner/author(s). licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License. © 2020 Copyright held by the owner/author(s). 2475-1421/2020/6-ART70 https://doi.org/10.1145/3386320 Proc. ACM Program. Lang., Vol. 4, No. HOPL, Article 70. Publication date: June 2020. 70:2 Bjarne Stroustrup Contents Abstract 1 Contents 2 1 Introduction 5 1.1 Chronology 6 1.2 Overview 7 2 Background: C++ 1979ś2006 7 2.1 The First Decade 7 2.2 The Second Decade 10 2.2.1 Language Features 10 2.2.2 Standard-Library Components 12 2.3 C++ in 2006 13 2.4 Other Languages 16 3 The C++ Standards Committee 17 3.1 The Standard 18 3.2 Organization 18 3.3 Impact on Design 21 3.4 Proposal Checklists 28 4 C++11: It Feels Like a New Language 29 4.1 C++11: Support for Concurrency 31 4.1.1 Memory Model 31 4.1.2 Threads and Locks 33 4.1.3 Futures 35 4.2 C++11: Simplifying Use 37 4.2.1 auto and decltype 38 4.2.2 Range-for 41 4.2.3 Move Semantics 42 4.2.4 Resource-Management Pointers 44 4.2.5 Uniform Initialization 45 4.2.6 nullptr 48 4.2.7 constexpr Functions 48 4.2.8 User-Defined Literals 50 4.2.9 Raw literals 51 4.2.10 Attributes 51 4.2.11 Garbage Collection 52 4.3 C++11: Improving Support for Generic Programming 52 4.3.1 Lambda 53 4.3.2 Variadic Templates 55 4.3.3 Aliases 56 4.3.4 tuples 58 4.4 C++11: Increase Static Type Safety 59 4.5 C++11: Support for Library Building 60 4.5.1 Implementation Techniques 60 4.5.2 Metaprogramming Support 61 4.5.3 noexcept Specifications 61 4.6 C++11: Standard-Library Components 62 5 C++14: Completing C++11 63 Proc. ACM Program. Lang., Vol. 4, No. HOPL, Article 70. Publication date: June 2020. Thriving in a Crowded and Changing World: C++ 2006ś2020 70:3 5.1 Digit Separators 64 5.2 Variable Templates 65 5.3 Function Return Type Deduction 65 5.4 Generic Lambdas 65 5.5 Local Variables in constexpr Functions 66 6 Concepts 67 6.1 The Prehistory of Concepts 68 6.2 C++0x Concepts 69 6.2.1 Concept Definitions 70 6.2.2 Concept Use 72 6.2.3 Concept Maps 72 6.2.4 Definition Checking 73 6.2.5 Lessons Learned 73 6.2.6 What Went Wrong? 75 6.3 The Concepts TS 77 6.3.1 Definition Checking 78 6.3.2 Concept Use 79 6.3.3 Concept Definition 81 6.3.4 Concept Name Introducers 82 6.3.5 Concepts and Types 82 6.3.6 Improvements 84 6.3.7 Syntax Equivalences 84 6.3.8 Why No Concepts in C++17? 86 6.4 C++20 Concepts 86 6.5 Naming of Concepts 88 7 Error Handling 89 7.1 Background 89 7.2 Real-World Problems 90 7.3 noexcept Specifications 92 7.4 Type System Support 92 7.5 Back to Basics 93 8 C++17: Lost at Sea 95 8.1 Constructor Template Argument Deduction 96 8.2 Structured Bindings 96 8.3 variant, optional, and any 99 8.4 Concurrency 100 8.5 Parallel STL 101 8.6 File System 102 8.7 Explicit Tests in Conditions 103 8.8 Proposals That Didn’t Make C++17 104 8.8.1 Networking 104 8.8.2 Operator Dot 104 8.8.3 Uniform Call Syntax 105 8.8.4 Default Comparisons 106 9 C++20: A Struggle for Direction 109 9.1 Design Principles 109 9.2 My C++17 List 110 9.3 C++20 Features 111 Proc. ACM Program. Lang., Vol. 4, No. HOPL, Article 70. Publication date: June 2020. 70:4 Bjarne Stroustrup 9.3.1 Modules 111 9.3.2 Coroutines 114 9.3.3 Compile-Time Computation Support 117 9.3.4 <=> 118 9.3.5 Ranges 119 9.3.6 Dates and Time Zones 120 9.3.7 Format 120 9.3.8 Span 121 9.4 Concurrency 123 9.5 Minor Features 123 9.6 Work in Progress 124 9.6.1 Contracts 124 9.6.2 Static Reflection 127 10 C++ in 2020 128 10.1 What Is C++ Used For? 129 10.2 The C++ Community 130 10.3 Education and Research 131 10.4 Tools 132 10.5 Programming Styles 133 10.5.1 Generic Programming 134 10.5.2 Metaprogramming 134 10.6 Coding Guidelines 136 10.6.1 General Approach 136 10.6.2 Static Analysis 137 11 Retrospective 139 11.1 The C++ Model 139 11.2 Technical Successes 139 11.3 Areas That Need Work 140 11.4 Lessons Learned 141 11.5 The Future 143 Acknowledgments 144 References 145 Proc. ACM Program. Lang., Vol. 4, No. HOPL, Article 70. Publication date: June 2020. Thriving in a Crowded and Changing World: C++ 2006ś2020 70:5 1 INTRODUCTION Originally, I designed C++ to answer to the question łHow do you directly manipulate hardware and also support efficient high-level abstraction?ž Over the years, C++ has grown from a relatively simple solution based on a combination of facilities from the C and Simula languages aimed at systems programming on 1980s computers to a far more complex and effective tool for an extraordinary range of applications. It retains its dual focus on: • Direct mapping of language constructs to hardware facilities • Zero-overhead abstraction This combination is the defining characteristic that sets C++ apart from most languages. łZero overheadž was explained like this [Stroustrup 1994]: • What you don’t use, you don’t pay for (aka łno distributed fatž).