Sparks

Modern C++, Part 1

Unlearning C++, Learning Modern C++, Part 1

I decided to update myself to Modern C++ from “Old C++” (ie C++98, C++20 with a smattering of later and older bits and pieces). One of the problems with this is that C++ has never been a small language, and the key question is where to start?

Do you choose to just update piecemeal? Or big bang? Do you treat it as a series of minor changes? I decided recently that while I knew/use aspects of modern C++, that my usage wasn’t idiomatic. Dijkstra made the comment that “programmers exposed to BASIC were damaged beyond all hope”. This wasn’t true, but there was an element of truth to it. People who learnt BASIC had to learn to unlearn certain habits. Someone learning a pure functional language needs to unlearn certain patterns from imperative (non functional) languages.

I realised that in order to properly learn idiomatic Modern C++, I need to conciously unlearn specific practices.

There’s no shortage of books on Modern C++, but I’m going to be using Scott Meyers “Effective Modern C++” for this journey.

My plan will be to work backwards from most recent versions of C++ and seeing how that changes your code. This is somewhat similar to the approach in Koenig & Moo’s Effective C++ which was a post-STL book and focussed on a STL-first style approach.

As part of doing this I’ll use the python Guild and Mini-Guild libraries I have to generate usecases for learning.

Some obvious starting points therefore:

  • Modern Smart Pointers, ownership, (de)allocation
  • Changes to for loops, iterators
  • Replacements to how modules / packages get built
  • Concurrency/Coroutines/Ranges/etc from C++20
  • Lambdas (added 20230714)
  • Auto typing
  • RAII
  • Packaging
  • CMake & Friends

Not all of these things are “new” and there were definitely “old” ways of doing a number of these. (especially thingings like smart pointers, RAII, etc) But that’s not the point. The point is around “idiomatic”. For example, while smart pointers are in books 23+ years old (inc Koenig & Moo), there are some specific more modern patterns of usage that are worth practicing, so I’ll cover that.

Each of these will include initially small examples. I don’t know how many posts I’ll have in this sequence, but it’ll be more than one :-)

Depending on context (and personal experience) I may cover how things used to be done versus now. However, I want to focus more on the now than the then so this may be limited to places which help explain the why of the new feature.

If comments make it clear there’s errors, I’ll update the posts, so corrections are very welcome.

Any suggestions for specific things to look at and cover will be welcome, but for the moment the above is the starting point.

Edit: Added lambdas, iterator changes

Updated: 2023/07/13 08:33:33