C++: A One-Day Overview (2024-11-19 - 2024-11-21)#
This was unusual: a one-day course based on A One-Day Overview Of C++, given three times in a row [1], to three different groups at the same company.
The day was woven around the screenplay, which I used to live-hack 2024-11-19/intro.cpp - which is the final outcome.
See the screenplay to see how we got there. Any deviations (asked questions, discussions) are documented below.
Course Flow: Questions And Discussions#
The following chapters correspond to sections in A One-Day Overview Of C++, which are referred to in their respective sidebars.
std::map, And Pitfalls (Pitfall: Encapsulate std::map Value In class Item)#
std::map, when filled using tdl["something"] = Item("... blah
..."), requires class Item to have a default constructor. This
is not entirely obvious, to say the least.
Problem: std::map::operator[]() creates a node with a
default-constructed value, which is then overwritten with
Item("... blah ...").
Solution: use std::map::insert(), or even better in our case at
least, use an initializer list and a const todo_list object.
Demo of the pitfall (std::map::operator[]()) in
2024-11-19/map-pitfall-no1.cpp
Brace Initialization#
Discussed the differences between {} and (). See
2024-11-19/brace-init.cpp
Smart Pointers, And Move Semantics#
While using std::shared_ptr<> in 2024-11-19/intro.cpp,
we approached the topic in
While we were at it, we discussed related topics like
Return value optimization (RVO) and Copy Elision. Demo code in 2024-11-19/rvo-or-not.cpp
Moving and rvalue references in general (Move Semantics, Rvalue References)
Dual implementation (move and copy) of, say,
std::vector::push_back(). Demo code in 2024-11-19/push-back-move.cpp
Lambda#
Transformed 2024-11-19/intro.cpp
into something that is more to the point. Talk about interfaces, the
cost of virtual (RTTI is not free), and readability in general.
For what a lambda really is, see the old school functor demo (2024-11-19/functor.cpp).
Use cases for lambda:
The optional third parameter,
Compare, tostd::sort(see 2024-11-19/sort-lambda.cpp)Creating threads, and more specifically, using std::async (see 2024-11-19/async.cpp)
Footnotes