Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code

Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code

Beautiful C++: 30 Core Guidelines for Writing Clean, Safe, and Fast Code
Автор: Davidson J. Guy, Gregory Kate
Дата выхода: 2022
Издательство: Addison-Wesley
Количество страниц: 773
Размер файла: 4.9 MB
Тип файла: PDF
Добавил: codelibs
 Проверить на вирусы

About This eBook....2

Halftitle Page....3

Title Page....4

Copyright Page....5

Pearson’s Commitment to Diversity, Equity, and Inclusion....6

Contents....8

Selected C++ Core Guidelines....11

Foreword....15

Preface....16

About This Book....18

Access the Code....20

Acknowledgments....21

About the Authors....23

Section 1: Bikeshedding is bad....25

Chapter 1.1. P.2: Write in ISO Standard C++....26

What is ISO Standard C++?....26

Encapsulating variations....28

Learning the old ways....32

Staying on top of developments to the standard....34

Chapter 1.2. F.51: Where there is a choice, prefer default arguments over overloading....37

Introduction....37

Refining your abstraction: Additional arguments or overloading?....38

The subtleties of overload resolution....40

Back to the example....42

The unambiguous nature of default arguments....44

Alternatives to overloading....46

Sometimes you must overload....47

Summary....48

Chapter 1.3. C.45: Don’t define a default constructor that only initializes data members; use in-class member initializers instead....49

Why have default constructors anyway?....49

How do you initialize a data member?....50

What happens when two people maintain a class?....54

Summary....57

Chapter 1.4. C.131: Avoid trivial getters and setters....58

An archaic idiom....58

Abstraction....59

Mere Encapsulation....62

Class Invariants....67

Nouns and Verbs....69

Summary....70

Chapter 1.5. ES.10: Declare one name (only) per declaration....71

Let me introduce you....71

Backward compatibility....75

Writing clearer declarations....77

Structured binding....78

Summary....79

Chapter 1.6. NR.2: Don’t insist to have only a single return-statement in a function....80

Rules evolve....80

Ensuring cleanup....82

Using RAII....85

Writing good functions....89

Summary....91

Section 2: Don’t hurt yourself....92

Chapter 2.1. P.11: Encapsulate messy constructs, rather than spreading through the code....93

All in one gulp....93

What it means to encapsulate a messy construct....96

The purpose of language and the nature of abstraction....98

Levels of abstraction....102

Abstraction by refactoring and drawing the line....103

Summary....104

Chapter 2.2. I.23: Keep the number of function arguments low....105

How much should they earn?....105

Simplifying matters through abstraction....107

Do as little as possible, but no less....111

Real-life examples....113

Summary....115

Chapter 2.3. I.26: If you want a cross-compiler ABI, use a C-style subset....116

Creating libraries....116

What is an ABI?....117

Paring back to the absolute minimum....119

Exception propagation....121

Summary....123

Chapter 2.4. C.47: Define and initialize member variables in the order of member declaration....125

Summary....135

Chapter 2.5. CP.3: Minimize explicit sharing of writable data....137

Traditional execution model....137

Wait, there’s more....140

Avoiding deadlocks and data races....142

Setting aside locks and mutexes....145

Summary....148

Chapter 2.6. T.120: Use template metaprogramming only when you really need to....149

std::enable_if => requires....159

Summary....164

Section 3: Stop using that....166

Chapter 3.1. I.11: Never transfer ownership by a raw pointer (T*) or reference (T&)....167

Using the free store....167

The performance cost of smart pointers....170

Using unadorned reference semantics....172

gsl::owner....173

Summary....176

Chapter 3.2. I.3: Avoid singletons....178

Global objects are bad....178

Singleton Design Pattern....179

Static initialization order fiasco....179

How to hide a singleton....182

But only one of these should ever exist....184

Wait a moment…....186

Summary....188

Chapter 3.3. C.90: Rely on constructors and assignment operators, not memset and memcpy....189

Chasing maximum performance....189

The horrendous overhead of the constructor....190

The simplest possible class....191

What is the standard talking about anyway?....194

But what about memcpy?....197

Never underestimate the compiler....199

Summary....201

Chapter 3.4. ES.50: Don’t cast away const....202

Story time....202

Dealing with rather more data....203

The const firewall....205

Implementing a dual interface....206

Caching and lazy evaluation....208

Two types of const....210

Surprises with const....212

Summary....214

Chapter 3.5. E.28: Avoid error handling based on global state (e.g. errno)....215

Error handling is hard....215

C and errno....215

Return codes....217

Exceptions....218

....219

Boost.Outcome....219

Why is error handling so hard?....221

Light at the end of the tunnel....223

Summary....224

Chapter 3.6. SF.7: Don’t write using namespace at global scope in a header file....225

Don’t do this....225

Disambiguation....226

Using using....227

Where do the symbols go?....229

An altogether more insidious problem....232

Solving the problem of cluttered scope resolution operators....234

The temptation and The Fall....236

Summary....237

Section 4: Use this new thing properly....238

Chapter 4.1. F.21: To return multiple “out” values, prefer returning a struct or tuple....239

The shape of a function signature....239

Documenting and annotating....240

Now you can return an object....242

You can also return a tuple....245

Passing and returning by non-const reference....249

Summary....253

Chapter 4.2. Enum.3: Prefer class enums over “plain” enums....254

Constants....254

Scoped enumerations....257

Underlying type....259

Implicit conversion....261

Summary....263

Postscript....264

Chapter 4.3. ES.5: Keep scopes small....265

The nature of scope....265

Block scope....266

Namespace scope....268

Class scope....271

Function parameter scope....273

Enumeration scope....274

Template parameter scope....276

Scope as context....277

Summary....278

Chapter 4.4. Con.5: Use constexpr for values that can be computed at compile time....279

From const to constexpr....279

Default C++....282

Using constexpr....283

inline....288

consteval....290

constinit....291

Summary....292

Chapter 4.5. T.1: Use templates to raise the level of abstraction of code....294

Story time....294

Raising the level of abstraction....297

Function templates and abstraction....299

Class templates and abstraction....302

Naming is hard....304

Summary....305

Chapter 4.6. T.10: Specify concepts for all template arguments....307

How did we get here?....307

Constraining your parameters....310

How to abstract your concepts....314

Factoring via concepts....317

Summary....319

Section 5: Write code well by default....321

Chapter 5.1. P.4: Ideally, a program should be statically type safe....322

Type safety is a security feature of C++....322

Union....324

Casting....326

Unsigned....330

Buffers and sizes....333

Summary....335

Chapter 5.2. P.10: Prefer immutable data to mutable data....336

The wrong defaults....336

constness in function declarations....339

Summary....344

Chapter 5.3. I.30: Encapsulate rule violations....346

Hiding the unsightly things in life....346

Keeping up appearances....348

Summary....355

Chapter 5.4. ES.22: Don’t declare a variable until you have a value to initialize it with....357

The importance of expressions and statements....357

C-style declaration....358

Declare-then-initialize....359

Maximally delayed declaration....361

Localization of context-specific functionality....364

Eliminating state....367

Summary....369

Chapter 5.5. Per.7: Design to enable optimization....370

Maximizing the frame rate....370

Working further from the metal....372

Optimization through abstraction....376

Summary....379

Chapter 5.6. E.6: Use RAII to prevent leaks....380

Deterministic destruction....380

Leaking away files....383

Why are we bothering?....387

This all seems a bit much: Future possibilities....389

Where can I get this?....393

Envoi....396

Afterword....398

Index....400

Code Snippets....431

Writing great C++ code needn't be difficult. The C++ Core Guidelines can help every C++ developer design and write C++ programs that are exceptionally reliable, efficient, and well-performing. But the Guidelines are so jam-packed with excellent advice that it's hard to know where to start. Start here, with Beautiful C++.Expert C++ programmers Guy Davidson and Kate Gregory identify 30 Core Guidelines you'll find especially valuable and offer detailed practical knowledge for improving your C++ style. For easy reference, this book is structured to align closely with the official C++ Core Guidelines website.Throughout, Davidson and Gregory offer useful conceptual insights and expert sample code, illuminate proven ways to use both new and longstanding language features more successfully, and show how to write programs that are more robust and performant by default.

  • Avoid "bikeshedding": stop wasting valuable time on trivia
  • Don't hurt yourself by writing code that will cause problems later
  • Know which legacy features to avoid and the modern features to use instead
  • Use newer features properly, to get their benefits without creating new problems
  • Default to higher-quality code that's statically type-safe, leak resistant, and easier to evolve
  • Use the Core Guidelines with any modern C++ version: C++20, C++17, C++14, or C++11

There's something here to improve virtually every program you write, design, or maintain.


Похожее:

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

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