You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapters/02_introduction.tex
+8-2Lines changed: 8 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -10,11 +10,12 @@ \section{History of standard \texorpdfstring{\CC}{C++} algorithms}
10
10
11
11
The \CC98 standard introduced most of the algorithms. However, it was the \CC11 standard with its introduction of lambdas that made algorithms worthwhile. Before lambdas, the time investment of writing a custom function object made the usefulness of algorithms dubious.
12
12
13
-
\begin{box-note}
13
+
\raggedbottom
14
+
\begin{box-nobreak}
14
15
\footnotesize Example of \cpp{std::for_each}\index{\cpp{std::for_each}} algorithm with a custom function object, calculating the number of elements and their sum.
\footnotesize Example of \cpp{std::for_each}\index{\cpp{std::for_each}} algorithm with a capturing lambda, calculating the number of elements and their sum.
@@ -153,3 +154,8 @@ \section{A simpler mental model for iterators}
153
154
154
155
Even when the algorithm returns an iterator to a specific element, it might be worth considering the implied range.
155
156
157
+
\begin{box-note}
158
+
\footnotesize Example of \cpp{std::find} establishing a prefix range that doesn't contain the searched-for element.
\item The presentation of each algorithm will start with a short description.
9
9
\item The margin will contain information about the history of the algorithm: which C++ standard introduced it and whether it has constexpr, parallel and range variants and including versions of the standard that introduced them.
10
-
\item Following that will be the description of constraints. Algorithms that write data to a distinct output range are denoted with an arrow: \cpp{input_range -> output_range}.
10
+
\item Following that will be the description of constraints. Algorithms that write data to a distinct output range are denoted with an arrow: \newline\cpp{input_range -> output_range}.
11
11
\item Finally, each description will conclude with one or more examples with explanations.
However, there are still a few corner cases when using \cpp{std::for_each} is preferable.
54
54
55
-
The first case is straightforward parallelization. Invoking an expensive operation for each element in parallel is trivial with \cpp{std::for_each}. On top of that, as long as the operations are independent, there is no need for synchronization primitives.
55
+
The first case is straightforward parallelization. Invoking an expensive operation for each element in parallel is trivial with \cpp{std::for_each}. As long as the operations are independent, there is no need for synchronization primitives.
56
56
57
57
\begin{box-note}
58
58
\footnotesize Example of a parallel \cpp{std::for_each} invoking a method on each element independently in parallel.
Second, the range version can provide a more concise and explicit syntax in some cases because of the projection support introduced in C++20.
64
64
65
65
\begin{box-note}
66
-
\footnotesize Example of the range version \cpp{std::ranges::for_each} utilizing a projection to iterate over the result of invoking the method \cpp{getValue()} on each element.
66
+
\footnotesize Example of the range version of \cpp{std::ranges::for_each} utilizing a projection to invoke the method \cpp{getValue()} (line 13) on each element and summing the resulting values using a lambda (line 12).
In this chapter, we will go over relevant in-depth topics referenced throughout the rest of the book.
4
+
5
+
\section{Argument-dependent lookup (ADL)}
6
+
7
+
When calling a method without qualification (i.e. not specifying the namespace), the compiler needs to determine the set of candidate functions. As a first step, the compiler will do an unqualified name lookup, which starts at the local scope and examines the parent scopes until it finds the first instance of the name (at which point it stops).
8
+
9
+
\begin{box-note}
10
+
\footnotesize Example of unqualified lookup. Both calls to \cpp{some_call} will resolve to \cpp{::A::B::some_call} since this is the first instance discovered by the compiler.
0 commit comments