diff --git a/exercises/basicTypes/PrintHelper.h b/exercises/basicTypes/PrintHelper.h index 073729bc..7df4c126 100644 --- a/exercises/basicTypes/PrintHelper.h +++ b/exercises/basicTypes/PrintHelper.h @@ -5,6 +5,12 @@ #include #include +/* + * 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 @@ -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"; } diff --git a/exercises/basicTypes/basicTypes.cpp b/exercises/basicTypes/basicTypes.cpp index 3a46116b..b67bccb8 100644 --- a/exercises/basicTypes/basicTypes.cpp +++ b/exercises/basicTypes/basicTypes.cpp @@ -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); diff --git a/exercises/basicTypes/solution/basicTypes.sol.cpp b/exercises/basicTypes/solution/basicTypes.sol.cpp index f8b94551..047c5d89 100644 --- a/exercises/basicTypes/solution/basicTypes.sol.cpp +++ b/exercises/basicTypes/solution/basicTypes.sol.cpp @@ -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 ); diff --git a/exercises/loopsRefsAuto/loopsRefsAuto.cpp b/exercises/loopsRefsAuto/loopsRefsAuto.cpp index bc0e5ced..0cb63fa4 100644 --- a/exercises/loopsRefsAuto/loopsRefsAuto.cpp +++ b/exercises/loopsRefsAuto/loopsRefsAuto.cpp @@ -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. diff --git a/exercises/loopsRefsAuto/solution/loopsRefsAuto.sol.cpp b/exercises/loopsRefsAuto/solution/loopsRefsAuto.sol.cpp index df04f05c..b248f843 100644 --- a/exercises/loopsRefsAuto/solution/loopsRefsAuto.sol.cpp +++ b/exercises/loopsRefsAuto/solution/loopsRefsAuto.sol.cpp @@ -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 ) { diff --git a/talk/basicconcepts/classenum.tex b/talk/basicconcepts/classenum.tex index 6f4ebf33..aabdcc6c 100644 --- a/talk/basicconcepts/classenum.tex +++ b/talk/basicconcepts/classenum.tex @@ -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 { diff --git a/talk/morelanguage/lambda.tex b/talk/morelanguage/lambda.tex index c9e7309c..7c064e4e 100644 --- a/talk/morelanguage/lambda.tex +++ b/talk/morelanguage/lambda.tex @@ -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}} @@ -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 diff --git a/talk/morelanguage/raii.tex b/talk/morelanguage/raii.tex index 710a6be2..b895fe07 100644 --- a/talk/morelanguage/raii.tex +++ b/talk/morelanguage/raii.tex @@ -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 diff --git a/talk/morelanguage/stl.tex b/talk/morelanguage/stl.tex index fb1b3ccc..ea8a3af4 100644 --- a/talk/morelanguage/stl.tex +++ b/talk/morelanguage/stl.tex @@ -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 diff --git a/talk/morelanguage/templates.tex b/talk/morelanguage/templates.tex index e212564b..81c132a4 100644 --- a/talk/morelanguage/templates.tex +++ b/talk/morelanguage/templates.tex @@ -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 @@ -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}