CppNorth - Weekend Edition
I got up early yesterday and drove to Toronto for the first day of CppNorth, and I’m attending an extra weekend session before the conference officially begins on Monday. I took a class taught by Patrice Roy called Generic Programming: from Strange to Normal.
This course exceeded my expectations. Patrice has participated in the C++ standards committee and has a language designer’s insight into some of the newer C++ features. I’ve been programming with C++ for over 20 years, which isn’t as advantageous for learning C++ as one might think because modern C++ development looks very different from what I was writing in the late 1990s. Even the review of the fundamentals of C++ generic programming had a surprising amount of content that he clarified or was new for me, which was great.
Patrice even kindly included some comments on the Curiously Recurring Template Pattern (CRTP), which I used in Hadron, so I wanted to hear his thoughts. He also covered a few topics from C++20, which I haven’t had much exposure to yet. In particular, Concepts seem helpful for the type-focused programming I’m doing in Hadron. I left the course feeling overwhelmed by all the ground we covered and inspired to uplevel my C++ code. I want to take better advantage of the modern language features to write more expressive, elegant, faster, and safer code than ever before. I found his course completely worthwhile. If you have a chance to take a class from Patrice, I’d highly recommend it.
Here’s an incomplete list of the topics Patrice covered that I either reviewed or learned something new about:
- A reminder to put
noexcept
on constructors, move assignments, and operator swap (if provided). template<class T> void foo(T&&)
is not always amove
reference; it’s a forwarding reference.- Any function returning a
T
after returning the generic class can result in corrupted state. This is whystd::stack::pop
doesn’t return aT
. - Stop writing raw
for
loops, usestd::algorithm
. The compiler can optimize more aggressively. I want to substantiate this claim with some experimentation and further research, but I also get the arguments for higher-level programming and clarity. - Fold expressions open up creative opportunities for variadic templates.
- Along with
std::cout
andstd::cerr
there’s a globalstd::clog
that can be assigned to anostream
and used for logging. emplace_back
is only faster thanpush_back
if you create the object in the function call, thus skipping a constructor and a copy.- I learned about
if constexpr
, which I hadn’t seen before.
And many others.