Skip to content
8 changes: 7 additions & 1 deletion exercises/basicTypes/PrintHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include <iomanip>
#include <string>

/*
* NOTE: You don't need to understand the print helpers here.
* Their purpose is to show each expression, the type it evaluates to, and the value it evaluates to.
* Please go back to the main file now. :-)
*/

#ifdef _MSC_VER
std::string demangle(std::string_view input) { return std::string{input}; }
#else
Expand All @@ -22,7 +28,7 @@ void printWithTypeInfo(std::string expression, auto const & t, bool useBitset =

std::cout << std::left << std::setw(30) << expression << " type=" << std::setw(20) << realname << "value=";
if (useBitset) {
std::cout << std::bitset<16>(t) << "\n";
std::cout << "0b" << std::bitset<16>(t) << "\n";
} else {
std::cout << std::setprecision(25) << t << "\n";
}
Expand Down
2 changes: 1 addition & 1 deletion exercises/basicTypes/basicTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main() {
bool condition1 = false;
bool condition2 = true;
print( alwaysTrue && condition1 && condition2 );
print( alwaysTrue || condition1 && condition2 ); // Q: Why does operator precedence render this expression useless?
print( alwaysTrue || condition1 && condition2 ); // Q: Are this and the following expressions useful?
print( alwaysTrue && condition1 || condition2 );
print(condition1 != condition1); // Q: What is the difference between this and the following expression?
print(condition2 = !condition2);
Expand Down
5 changes: 3 additions & 2 deletions exercises/basicTypes/solution/basicTypes.sol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ int main() {
bool condition1 = false;
bool condition2 = true;
print( alwaysTrue && condition1 && condition2 );
print( alwaysTrue || condition1 && condition2 ); // Q: Why does operator precedence render this expression useless?
print( alwaysTrue && condition1 || condition2 ); // A: "true || " is evaluated last. The expression therefore is always true.
print( alwaysTrue || condition1 && condition2 ); // Q: Are this and the following expressions useful?
// A: Not really. Since we use "true ||", it is always true.
print( alwaysTrue && condition1 || condition2 ); // A: "true && condition1" is the same as "condition1"
print(condition1 != condition1); // Q: What is the difference between this and the following expression?
print(condition2 = !condition2); // A: The first is a comparison, the second a negation with subsequent assignment
print( alwaysTrue && condition1 && condition2 );
Expand Down
3 changes: 2 additions & 1 deletion exercises/loopsRefsAuto/loopsRefsAuto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ int main() {
DontCopyMe collection[10];

// Task 1:
// Write a for loop that initialises each struct's resultA and resultB with ascending integers.
// Write a for loop that initialises resultA and resultB for each element in the above array
// with sensible numbers.
// Verify the output of the program before and after you do this.


Expand Down
3 changes: 2 additions & 1 deletion exercises/loopsRefsAuto/solution/loopsRefsAuto.sol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ int main() {
DontCopyMe collection[10];

// Task 1:
// Write a for loop that initialises each struct's resultA and resultB with ascending integers.
// Write a for loop that initialises resultA and resultB for each element in the above array
// with sensible numbers.
// Verify the output of the program before and after you do this.

for ( int i = 0 ; i<10 ; ++i ) {
Expand Down
2 changes: 1 addition & 1 deletion talk/basicconcepts/classenum.tex
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[98]{More sensible example}
\frametitlecpp[11]{More sensible example}
\begin{multicols}{2}
\begin{cppcode*}{}
enum class ShapeType {
Expand Down
32 changes: 31 additions & 1 deletion talk/morelanguage/lambda.tex
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,36 @@
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[11]{Capture by value vs.\ by reference}
\begin{exampleblock}{See the difference between val and ref}
\begin{cppcode*}{}
int data[]{1,9,3,8,3,7,4,6,5};
int increment = 3;
auto val = [ inc](int x) { return x+inc; };
auto ref = [&inc](int x) { return x+inc; };

increment = 4;

for(int& i : data) i = val(i); // increments by 3
for(int& i : data) i = ref(i); // increments by 4
\end{cppcode*}
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[14]{Init capture}
\begin{exampleblock}{Capture with an initializer}
In \cpp14, can declare captures with initializers
\begin{cppcode*}{}
auto f = [inc = 1+2](int x) { return x+inc; };
auto g = [inc = getInc()](int x) { return x+inc; };
for(int& i : data) i = f(i); // increments by 3
for(int& i : data) i = g(i); // unknown increment
\end{cppcode*}
\end{exampleblock}
\end{frame}

\begin{frame}[fragile]
\frametitlecpp[11]{Anatomy of a lambda}
\begin{block}{Lambdas are pure syntactic sugar - \cppinsightLink{https://cppinsights.io/s/67800da8}}
Expand Down Expand Up @@ -211,7 +241,7 @@
\end{column}
\end{columns}
\end{block}
\begin{exampleblock}{Some nice consequence}
\begin{exampleblock}{Some nice consequences}
\begin{itemize}
\item Lambda expressions create ordinary objects
\item They can be copied, moved, or inherited from
Expand Down
2 changes: 1 addition & 1 deletion talk/morelanguage/raii.tex
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@

\begin{frame}[fragile]
\frametitlecpp[11]{\texttt{std::shared\_ptr}}
\begin{block}{\mintinline{cpp}{std::shared_ptr} : a reference counting pointer}
\begin{block}{\mintinline{cpp}{std::shared_ptr} : a reference-counting pointer}
\begin{itemize}
\item wraps a regular pointer similar to \cppinline{unique_ptr}
\item has move and copy semantic
Expand Down
1 change: 1 addition & 0 deletions talk/morelanguage/stl.tex
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
m["hello"] = 1; // inserts new key, def. constr. value
m["hello"] = 2; // finds existing key
auto [it, isNewKey] = m.insert({"hello", 0}); // no effect
// ^ C++17: "Structured binding"
int val = m["world"]; // inserts new key (val == 0)
int val = m.at("monde"); // throws std::out_of_range

Expand Down
4 changes: 2 additions & 2 deletions talk/morelanguage/templates.tex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

\begin{frame}[fragile]
\frametitlecpp[98]{Templates}
\begin{alertblock}{Warning}
\begin{block}{Notes on templates}
\begin{itemize}
\item they are compiled for each instantiation
\item they need to be defined before used
Expand All @@ -38,7 +38,7 @@
\end{itemize}
\item this may lead to longer compilation times and bigger binaries
\end{itemize}
\end{alertblock}
\end{block}
\newsavebox{\codepiece}
\begin{lrbox}{\codepiece}
\begin{minipage}{.35\linewidth}
Expand Down