Table of Contents
Beautiful C++ - 30 Core Guidelines for Writing Clean, Safe, and Fast Code Index
Return to Beautiful C++, Beautiful C++ Table of Contents
Index
A
cross-compiler, 79–85
purpose of, 80–81
abstract machine, 143–145, 165
abstraction
aliasing namespaces, 176–177
in API design, 13–14
buffers, 256
class templates and, 231–233
of concepts, 240–242
declarations and, 45
in enumerations, 269–273
examples of usage, 273–274
function templates and, 229–231
history of, 32–34
levels of, 68–69
messy constructs example, 65–68
minimizing function arguments, 73–75
in multithreaded programming, 104–105
naming, difficulty of, 233
nouns/verbs in, 39–40
optimization through, 290–292
purpose of, 32, 65, 273
raising level with templates, 225–233
by refactoring, 69–70
scope and, 210
single-instance, 135
ACCU (Association of C and C++ Users), 11
acyclic graphs, 172
aggregates
abstract machine optimization, 144–145
initializing, 141–143
<algorithm> header, 230–231
algorithms, repetition and, 69–70
aliasing
namespaces, 176–177
with using keyword, 171
alignment, class layout and, 89–91
Annotated Reference Manual (Ellis and Stroustrup), 4
annotations in function signatures, 182–183
anonymous namespace, 204–205
ANSI (American National Standards Institute), 4
API design
abstractions in, 13–14
self-documentation, 13
application binary interface (ABI)
cross-compiler, 79–85
purpose of, 80–81
arguments
default versus overloading, 13–21
function signatures, 181–182
minimizing number of, 71–78
parameters versus, 13–14
template arguments, concepts for, 235–243
unambiguous nature of default, 18–19
ARM. See Annotated Reference Manual (Ellis and Stroustrup)
array decay, 256
as-if rule, 94, 143–145, 185
asm declarations, 42
assembly language, levels of abstraction and, 227–228
assert macro, 166
assignment operators, preferring over memcpy, 139–148
Association of C and C++ Users (ACCU), 11
atomic objects, 101
attributes, declaring, 42
auto keyword, 8, 248
auto_ptr, 122
automatic storage duration, 293, 295
B
backward compatibility of C++, 9, 43–45, 215–216
BASIC language, 49–50
bit manipulation, 255
bit patterns, 247
bitwise const, 155–156, 261–262
block scope, 202–203
Boost classes for error handling, 163–164
buffer size, 255–257
built-in types, 82–83
C
C++ programming language. See also ISO Standard C++
defaults in, 215–216, 259–261
history of, 3–4
performance, 139–140
The C++ Programming Language (Stroustrup), 3
C++ Seasoning (Parent), 69–70
C++ Standards Committee, participation in, 303–304
caching, const keyword and, 154–155
casting
const, avoiding, 149–158
enumerations, 199–200
type safety and, 250–253
Cfront, 3
class encapsulation, 63
class enumerations, preferred over unscoped, 193–200
class invariants
minimizing function arguments, 73–75
purpose of, 37–39
class layout, alignment and, 89–91
class members, importing, 171
class scope, 206–207
class templates, abstraction and, 231–233
Cline, Marshall, 130–131
cohesion, 76
compilers
abstract machine, 143–145
proper usage of, 147–148
variations in, 5–6
compile-time computation, 213–223
consteval keyword, 221–222
constexpr usage examples, 216–220
constinit keyword, 222–223
default C++, 215–216
history of constexpr keyword, 213–215
inline keyword, 220–221
concepts
abstraction of, 240–242
factoring via, 242–243
in ISO Standard C++, 235
parameter constraints and, 237–240
problem solved by, 236–237
for template arguments, 235–243
<concepts> header, 239
concurrency, multithreaded programming, 97–105
conferences, 11
const firewall, 151–152
const keyword, 149–158
caching and, 154–155
const firewall, 151–152
default C++ and, 215–216, 259–261
dual interface implementation, 152–154
in function declarations, 261–265
history of constexpr keyword, 213–215
logical versus bitwise const, 155–156
maintaining state, 149–151
pointers to const versus const pointers, 157–158
preferring immutable over mutable data, 259–265
const_cast keyword, 149–158, 252
constant initialization, 222–223
constants
enumerations and, 194–195
as preprocessor macros, 193–194
consteval keyword, 221–222
constexpr if statement, 115–116
constexpr keyword
history of, 213–215
usage examples, 216–220
constinit keyword, 222–223
constraints on parameters, 237–240
constructors
default, purpose of, 23–24
default parameters, 29
member data initialization, 93–94
multiple, 27–28
performance overhead, 140–141
preferring over memset, 139–148
private, 133
context-specific functionality, localization of, 280–282
contracts, 166
conversions
implicit, 198–200
in standard conversion sequences, 16–17
copy elision, 184–186
CppCon, 11
Cpre, 3
cross-compiler ABIs, 79–85
C-style casting, 251–252
C-style declaration, 276–277
C-style subsets, 82–83
D
- DAG (directed acyclic graph), 172–173
daisy-chaining functions, 190–191
data privacy
in abstraction, 39–40
with encapsulation, 34–37
data races
avoiding, 101–103
definition of, 98–99
data sources, member functions of, 73–75
deadlocks
avoiding, 101–103
definition of, 100
debugging libraries, 79–80
declarations
abstraction and, 45
backward compatibility, 43–45
const keyword in, 261–265
C-style, 276–277
declare-then-initialize, 277–278
delaying, 275–283
maximally delayed, 278–280
multiple, avoiding, 41–46
order of initialization, 87–95
purpose of, 41
structured binding, 46
types of, 41–43
declare-then-initialize style, 277–278
default arguments
overloading versus, 13–21
unambiguous nature of, 18–19
default C++, 215–216, 259–261
default constructors
multiple, 27–28
purpose of, 23–24
default member initializers, 26, 28
default parameters in constructors, 29
delaying declarations, 275–283
C-style declaration versus, 276–277
declare-then-initialize, 277–278
localization of context-specific functionality, 280–282
maximally delayed, 278–280
state, eliminating, 282–283
design for optimization, 285–292
design patterns, 130
Design Patterns, 130, 135
deterministic destruction, 92–93, 201, 293–295
- Dijkstra, Edsger, 51
directed acyclic graph (DAG), 172–173
directed graphs, 172
Discord chats, 11
documentation with SAL, 182–183
dual interface implementation, 152–154
duck typing, 247
Dusíková, Hana, 303–304
dynamic allocation, 8
dynamic storage duration, 293, 295
dynamic_cast keyword, 252
E
Elements of Programming (Stepanov), 239–240
Ellis, Margaret, 4
enable_if clause, 114–117
encapsulation
with concepts, 240
information hiding and, 64–65
with namespaces, 170–171
purpose of, 34–37, 63–64
of rule violations, 267–273
enumeration scope, 208–209
enumerations
abstraction in, 269–273
constants and, 194–195
encapsulation, 63
implicit conversion, 198–200
purpose of scoped, 196–197
scoped versus unscoped, 193–200
underlying type, 197–198
errno object, 159–160
error handling, 186–188
avoiding based on global state, 159–166
Boost classes, 163–164
errno object, 159–160
exceptions, 162
proposals for, 166
return codes, 161
<system_error> header, 162–163
types of errors, 164–165
exact match standard conversion sequences, 16
exception handling
exception propagation, 84–85
RAII, 53–55
exception propagation, 84–85
exceptions
in error handling, 162
zero-overhead deterministic exceptions, 166
exchanging messages, 103–104, 262
<experimental/scope> 300–303
explicit conversion of enumerations, 199–200
explicit sharing of writable data, minimizing, 97–105
expression templates, 110–113
expressions, importance of, 275–276
extensions to C++, 6
F
factoring via concepts, 242–243
file handling, 295–298
file scope, 32–33
forward compatibility of C++, 9–10
frame rate, maximizing, 285–286
“The Free Lunch Is Over” (Sutter), 100
free store, 121–123
function arguments. See arguments
function overloading
alternatives to, 19–20
default arguments versus, 13–21
necessity of, 20–21
overload resolution, 15–17
function parameter scope, 207–208
function signatures
annotations in, 182–183
in-out parameters, 188–191
input/output parameters, 181–182
objects, returning, 183–186
tuples, returning, 186–188
function templates
abstraction and, 229–231
arguments, concepts for, 235–243
parameter constraints, 237–240
problem solved by concepts, 236–237
function-body initialization, 25–26
functions
abstraction, 65–68
cleanup, 51–53
compile-time computation, 213–223
daisy-chaining, 190–191
declaring, 41, 261–265
encapsulation, 63
exception handling, 53–55
good coding practice, 56–57
input/output parameters, 181–182
messy constructs example, 61–63, 268–269
naming, 34
“one function, one responsibility,” 75–76
overloading in namespaces, 175–176
pure, 56
simplicity of, 56
single-return rule, avoiding, 49–57
fundamental types, variations in, 7–8
G
- Gamma, Erich, 135
business logic in, 36–37
class invariants, 37–39
encapsulation and data privacy, 34–37
purpose of, 31–32
trivial, avoiding, 31–40
global namespace, 129
scope, 204–205
using directives in, avoiding, 169–178
global objects
avoiding, 129
when to use, 136–137
global state, error handling based on, 159–166
Goldberg, Rube, 134
graphs, 172
GSL (Guidelines Support Library), 77, 126–128
H
Hacker’s Delight, 117
header files
header guards, 7
history of abstraction, 33–34
header guards, 7
hiding singletons, 132–134
history
of abstraction, 32–34
of C++, 3–4
of constexpr keyword, 213–215
Hoare, Tony, 290
Hyrum’s Law, 33
I
IILE (Immediately Invoked Lambda Expression), 94, 269, 281–282
immutable data, preferring over mutable, 259–265
implicit conversion of enumerations, 198–200
implicit conversion sequences, ranking, 16–17
importing class members, 171
information hiding, 64–65
initialization
of aggregates, 141–143
constinit keyword, 222–223
in C-style declaration, 276–277
declare-then-initialize, 277–278
default member initializers, 26, 28
delaying declaration, 275–283
function-body, 25–26
importance of, 24–25
in initializer list, 26
maximally delayed declaration, 278–280
order of, 87–95
RAII, 53–55, 270–273, 293–303
static initialization order fiasco, 130–132
two-phase, purpose of, 23–24
initializer list, initialization in, 26
inline keyword, 220–221
inline namespace, 205–206
inlining, 286
in-out parameters, 188–191
input parameters, 181–182
int type
enumerations and, 198–200
for money, 71
variations in, 7–8
intent, declaring, 229
interfaces
class scope, 206–207
dual interface implementation, 152–154
static, 134
iostream library, 190, 296–297
ISO Standard C++
abstract machine, 143–145
backward compatibility, 9, 43–45
C++ Standards Committee participation, 303–304
concepts in, 235
forward compatibility, 9–10
history of C++, 3–4
resources for information, 10–11
variation encapsulation, 4–8
- IsoCpp, 10
K
auto, 8, 248
const. See const keyword
const_cast, 149–158, 252
consteval, 221–222
constexpr, 213–215
constinit, 222–223
dynamic_cast, 252
inline, 220–221
mutable, 156–157
reinterpret_cast, 252–253
requires, 242
static_cast, 251–252
union, 249–250
unsigned, 253–255
using, 8, 171–172
virtual, 44–45
- Knuth, Donald, 290–291
L
constraints, 238
IILE, 269, 281–282
initialization, 94–95
language level, variations in, 5–6
late function binding, 44–45
LCA (lowest common ancestor), 173–174
leading punctuation style, 93–94
leaks
in file handling, 295–298
future prevention possibilities, 300–303
memory, 121, 293–295
reasons for preventing, 298–300
Lenkov, Dmitry, 4
levels of abstraction, 68–69
purpose of, 227–228
raising with templates, 225–233
libraries
ABI and, 80–81, 84–85
creating, 79–80
debugging, 79–80
lifetime of objects, 202, 204–205, 293–295
linkages
declaring, 42
scope and storage duration and, 204–205
linkers, 132
localization of context-specific functionality, 280–282
locking mutexes, 101–103, 156
logical const, 155–156, 261–262
loop unrolling, 286
lowest common ancestor (LCA), 173–174
M
Mastering Machine Code on Your ZX81 (Baker), 49
maximally delayed declaration, 278–280
maximizing
frame rate, 285–286
performance, 139–140
Mechanization of Contract Administration Services (MOCAS), 85
Meeting C++, 11
member data initialization
in aggregates, 142–143
default member initializers, 26, 28
function-body, 25–26
importance of, 24–25
in initializer list, 26
order of, 87–95
member functions
of data sources, 73–75
as mutable, 263
memcpy, 114–115, 139–148
memory
buffer size, 255–257
free store, 121–123
realloc function, 75–76
memory leaks, 121, 293–295
memset, avoiding, 139–148
merge function, 77
messages, exchanging, 103–104, 262
messy constructs
abstraction, 65–68
encapsulation and information hiding, 63–65
example of, 61–63, 268–269
metaprogramming, template, 107–117
complexity of, 107–108
expression templates, 110–113
memcpy, 114–115
self-modifying code, 108–110
Meyers, Scott, 132, 200
Meyers Singleton, 132
millennium bug, 9–10
minimizing
explicit sharing of writable data, 97–105
number of function arguments, 71–78
scope, 201–210, 275
MOCAS (Mechanization of Contract Administration Services), 85
Model-View-Controller, 39
modules, encapsulation, 63–64
money, int type for, 71
Moore’s Law, 100
multiple constructors, 27–28
multiple declarations, avoiding, 41–46
multiple processors, multithreaded programming with, 99–101
multiple return statements, 49–57
multithreaded programming, 97–105
abstraction in, 104–105
data races and deadlocks, avoiding, 101–103
exchanging messages, 103–104
with multiple processors, 99–101
traditional model, 97–99
mutable data, preferring immutable over, 259–265
mutable keyword, 156–157
mutexes, locking, 101–103, 156
N
name mangling, 81
named return value optimization (NRVO), 185–186
namespace aliases, declaring, 42
namespace scope, 203–206
namespaces
aliasing, 176–177
declaring, 42
encapsulation, 63–64, 170–171
global, 129
nested, 172–174
overloaded functions in, 175–176
singletons as, 135
using directives at global scope, avoiding, 169–178
naming
concepts, 240
difficulty of, 233, 239
functions, 34
nested namespaces, 172–174
nested scope, 203
no_discard attribute, 215–216
nouns/verbs in abstraction, 39–40
- NRVO (named return value optimization), 185–186
O
objects
declaring, 43
global, 129, 136–137
lifetime (storage duration), 202, 204–205, 293–295
returning, 183–186
“one function, one responsibility,” 75–76
opaque enum declarations, 43
optimization
abstract machine and, 143–145
compiler usage and, 147–148
design for, 285–292
maximizing frame rate, 285–286
RVO and NRVO, 185–186
sort function example, 286–290
through abstraction, 290–292
order of initialization, 87–95
OSI model, 68
output parameters
in function signatures, 181–182
objects, returning, 183–186
tuples, returning, 186–188
overload resolution, 15–17
overloading
alternatives to, 19–20
default arguments versus, 13–21
in namespace functions, 175–176
necessity of, 20–21
ownership, transferring, 121–128, 269
free store, 121–123
GSL (Guidelines Support Library), 126–128
smart pointers, 122–125
unadorned reference semantics, 125–126
P
parameters
abstraction, 73–75
arguments versus, 13–14
constraints on, 237–240
default, in constructors, 29
documentation with SAL, 182–183
function parameter scope, 207–208
in-out, 188–191
input/output, 181–182
as mutable, 262–263, 264–265
template parameter scope, 209–210
Parent, Sean, 69–70
Pareto Principle, 291
performance
constructor overhead, 140–141
maximizing, 139–140
optimization for, 285–292
returning objects, 183–186
pointers
const, 157–158
as mutable, 263
raw, 17
smart, 122–125
transferring ownership, 121–128, 269
portability, levels of abstraction and, 227–228
- pragma once, 7
preprocessor macros, 193–195
preventing leaks, 298–300
privacy of data
in abstraction, 39–40
with encapsulation, 34–37
private constructors, 133
processor instructions, 139–140
programming bugs, 164–165
Programming the Z80 (Zaks), 49
promotion in standard conversion sequences, 16
public data in abstraction, 39–40
pure functions, 56
Q
Qt, 6
R
avoiding, 101–103
definition of, 98–99
RAII (Resource Acquisition Is Initialization), 53–55, 270–273, 293–303
file handling leaks, 295–298
future possibilities, 300–303
memory leaks, 293–295
reasons for preventing leaks, 298–300
ranges, identifying, 77–78
ranking implicit conversion sequences, 16–17
raw pointers, 17, 121–128, 269
realloc function, 75–76
recoverable errors, 164
refactoring, abstraction by, 69–70
reference
as mutable, 263
transferring ownership, 121–128, 269
reference-counted singletons, 133–134
reflection, 117
regulatory constraints, 8
reinterpret_cast keyword, 252–253
repetition, algorithms and, 69–70
requires clause, 116–117
requires keyword, 242
Resource Acquisition Is Initialization. See RAII (Resource Acquisition Is Initialization)
resources for information, 10–11
return codes, 161
return statements
cleanup, 51–53
const-qualifying, 264
in function signatures, 181–182
objects in, 183–186
single-return rule, avoiding, 49–57
tuples in, 186–188
Robinson, W. Heath, 134
rule violations, encapsulation of, 267–273
run-time environment, variations in, 4–5
RVO (return value optimization), 185–186
S
SAL (source code annotation language), 182–183
scope
block, 202–203
class, 206–207
context of, 210
enumeration, 208–209
function parameter, 207–208
future leak prevention possibilities, 300–303
minimizing, 201–210, 275
namespace, 203–206
nested, 203
purpose of, 201–202
template parameter, 209–210
types of, 202
scope creep, example of, 61–63, 268–269
scope resolution operators, 176–177, 204
scoped enumerations
preferred over unscoped, 193–200
purpose of, 196–197
self-documentation, 13, 34
self-modifying code, 108–110
setters. See getters/setters
SFINAE (Substitution Failure Is Not An Error), 115
shared_ptr, 122–125
simple declarations, 43
Single Entry, Single Exit, 50–51
single-instance abstractions, 135
single-return rule, avoiding, 49–57
singletons
avoiding, 129–137
as design pattern, 130
hiding, 132–134
as namespaces, 135
static initialization order fiasco, 130–132
static interfaces, 134
when to use, 135–137
smart pointers, 122–125
sort function, optimization of, 286–290
sortable concept, 241–242
source code annotation language (SAL), 182–183
source files, encapsulation, 63–64
ssize function, 255
stack manipulation, 286
Standard C++. See ISO Standard C++
standard conversion sequences, 16–17
state
eliminating, 282–283
error handling based on, 159–166
maintaining across platforms, 149–151
statements, importance of, 275–276
static initialization order fiasco, 130–132
static interfaces, 134
static storage duration, 293
static_assert declarations, 43
static_cast keyword, 251–252
Stepanov, Alex, 239–240
storage duration of objects, 202, 204–205, 293–295
string literals, 78
Stroustrup, Bjarne, 3–4, 307
structured binding, 46, 187–188
Structured Design (Yourdon and Constantine), 76
Structured Programming (Johan-Dahl, Dijkstra, Hoare), 51
“Structured Programming with go to Statements” (Knuth), 290–291
subsets, C-style, 82–83
Substitution Failure Is Not An Error (SFINAE), 115
Sutter, Herb, 100, 307–308
synchronization, maintaining across platforms, 149–151
<system_error> header, 162–163
T
tags, 272–273
tasks, threads as, 104–105
taxonomy of types, 239–240
TCPL. See The C++ Programming Language (Stroustrup)
template instantiations, declaring, 42
template metaprogramming (TMP), 107–117
complexity of, 107–108
expression templates, 110–113
memcpy, 114–115
self-modifying code, 108–110
template parameter scope, 209–210
templates
arguments, concepts for, 235–243
class templates, 231–233
function templates, 229–231
naming, difficulty of, 233
raising level of abstraction, 225–233
thread-local storage duration, 293
threads
multithreaded programming, 97–105
as tasks, 104–105
TMP. See template metaprogramming (TMP)
“train model” for ISO Standard C++, 4
transferring ownership, 121–128, 269
free store, 121–123
GSL (Guidelines Support Library), 126–128
smart pointers, 122–125
unadorned reference semantics, 125–126
trivial getters/setters
avoiding, 31–40
encapsulation and data privacy, 34–37
tuples, returning, 186–188
two-phase initialization, purpose of, 23–24
type aliases, declaring, 42
type punning, 250
type safety
buffer size, 255–257
casting, 250–253
purpose of, 247–248
union keyword, 249–250
unsigned keyword, 253–255
types
built-in, 82–83
for enumerations, 197–198
for money, 71
as mutable, 263
taxonomy of, 239–240
variations in, 7–8
U
underlying type for enumerations, 197–198
union keyword, 249–250
unique_ptr, 122–124, 297–298
unscoped enumerations
implicit conversion, 198–200
preferring scoped over, 193–200
unsigned keyword, 253–255
user-defined conversion sequences, 17
using declarations, 42, 171–172
using directives, 43
at global scope, avoiding, 169–178
in nested namespaces, 172–174
overloaded namespace functions and, 175–176
purpose of, 172
using enum declarations, 43
using keyword, 171–172, 307–308
V
variables
C-style declaration, 276–277
declare-then-initialize, 277–278
delaying declaration, 275–283
maximally delayed declaration, 278–280
variant object, 249
variation encapsulation
extensions to C++, 6
fundamental types, 7–8
header files, 7
language level and compiler, 5–6
regulatory constraints, 8
run-time environment, 4–5
vectors, 233
verbs/nouns in abstraction, 39–40
virtual keyword, 44–45
W
writable data, minimizing explicit sharing, 97–105
Y
Y2K bug, 9–10
Z
Z80 assembly language, 49–50, 285–286
zero initialization, 222–223
zero-overhead deterministic exceptions, 166
Fair Use Sources
© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers
SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.