How to master C++ in 2021

How to cast yourself as a C++ pro without segfaulting

Ruvinda Dhambarage
5 min readMar 20, 2021
https://xkcd.com/292/

C++ is easy to pickup and read. Features like mutable by default and implicit conversions, are powerful features that make it easy to write “close to the metal code”. However it is also very easy to shoot yourself in the foot by writing sub-optimum code that generates unnecessary machine instructions and/or memory copies. It’s also very easy to write code that is hard to extend and test; resulting in hard to maintain code.

While the language spec is now 40 years old, it is currently in the midst of an accelerated evolution that is rapidly introducing new features. Thus we see that “modern” C++ code looks a lot differnt than pre 2010 C++ code. So, if you are total noob or starting afresh with C++ after a long hiatus (like me); it’s hard to get a baring of all that you need to know.

In this post I will attempt to list most of the features that one should know. I will also try to link to learning resources were possible. My goal is to list topics and give and idea of the scope; I will leave it to the reader to Google topics and follow the rabbit hole .

Photo by Gia Oris on Unsplash

Beginner : Understanding core features

Must know language features:

  • Uniform initialization (curly brace initialization)
  • Pointers, references & smart pointers
    * unique, shared & weak pointers
  • const correctness : https://isocpp.org/wiki/faq/const-correctness
  • Range based loops
  • auto : type deduction
  • std::string vs std::string_view
  • Exceptions : how to use and when to use
  • constexpr
    * Compile time evaluation : learncpp.com
  • Lambdas : learncpp.com
  • Trailing return type syntax
    * Return type deduction : stackoverflow.com
  • Move and Copy semantics
    * lvalues and rvalues
  • Templates : Static (compile time) Polymorphism
  • Inheritance : Dynamic (run time) Polymorphism
    * Object splicing

Recommended resources :

Photo by Bruno Nascimento on Unsplash

Intermediate: Architecture & Design

Don’t reinvent the wheel..

Know what is available in the standard libraries

  • std data structures: std::optional, std::pair, std::tuple, std::any, std::variant..
  • std containers : std::array, std::vectors, std::maps..
  • std algorithms : std::copy, std::swap, std::sort..
  • Boost
  • GSL (Guidelines support library)

Architecture

  • SOLID design principles : Still the best design guidelines
  • Design patterns:
    * Creational patterns (Factory, Singleton..)
    * Structural patterns (Adapter, Decorator..)
    * Behavioral patterns (Observer, Visitor..)
  • C++ specific design patterns :
    * Curiously Recurring Template Pattern (CRTP)
    * Mixin inheritance
  • Programing paradims : C++ is a generic language, OOP is just one option. Considor using other programming paradigms ilke “Data oriented Design”, “Functional programming”..

Recommended resources :

Intermediate : Use tools

There are many powerful tools for C++ that can detect and even fix common mistakes and guideline violations.

  • Sanitizers : Detect anomalize at runtime (memory leads, out of bound memry access, invoking undefined behaviour..)
  • Static analysis : Detect possible anomalize at compile time. Tools: clang-tidy, cppcheck
  • Advanced debugging tools : valgrind, rr
  • Build tools : CMake, ninja
  • Package managers : conan, vcpkg
  • Testing frameworks : Google Test, Catch2

Recommended resources :

Photo by Ameer Basheer on Unsplash

Advance : Optimizations

Must know optimizations

Looking under the hood

To truly master and write the most optimized C++ code you need to go down to the assembly level. It is when you study C++ compiler generated assembly code that you will understand the impact of abstractions and optimizations. In essence you need to think low level and write high level code.

Recommended resources :

Advance : Cutting edge features

New and more advanced features

  • Coroutines
  • Concepts / Traits
  • Fold expressions
  • Immediately invoked function expressions (IIFE)
  • Meta classes and Code injection

Recommended resources :

Continuous learning

It goes without saying that you need to be constantly learning to both keep up and learn new tricks. To that end I can recommend the following:

  1. Learn other languages : learn drastically differnt languages to expand your thinking
  2. Reddit: /r/cpp
  3. YouTube:
    * CppCon channel
    * C++ Weekly

4. Podcasts : https://cppcast.com/

PS: There is currently a Humble Bundle book deal that includes some of the books I’ve listed here. The offer expires 04/04/2021 https://www.humblebundle.com/books/learn-you-more-code-no-starch-press-books

Update: 25/09/2021

Adding a few extra resources that I can recommend

--

--