C++ is a big language that has evolved tremendously since it's inception way back in the 1980's.
Throughout the years, many million lines of code have been written in the language and a big portion of that code is using legacy features that aren't considered good practice anymore.
There were many attempts to replace the lanaguage. All of them failed as far as I know.
Some attempts were made to subset the language in order to get rid of code & language dept, which hurt speed and portability.
The most recent hype is around rust, which is a blazing fast, memory safe systems programming language. I see a promising future for
rust, and I'm actually learning it myself. But like Bjarne said in his talk Writing Good C++14, it would take ~10 years for a good language to make it to the mainstream.
C++ is already here. We need to find a way for people to write good C++ now.
Subset of Superset
Simply sub-setting the language won't work. This is backed by previous, failed, attempts. In order to maintain the speed of C++ we need the low-level / tricky / close-to-the-hardware / error-prone / expert-only features. Those feature are the building blocks of for higher-level facilitiese & libraries.
Bjarne talked about the subject at CPPCon a few years back: <iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/1OEu9C51K2A?rel=0&start=1063" frameborder="0" allowfullscreen></iframe>
Bjarne said we first need to create a superset of the langauge, then subset it in order to get rid of the crud. In order to do so, we need supporting facilities to make the transition: from guidelines on how to write modern C++, to libraries that enpasulate the usage of messy & dangerous things so most programmers won't need to use them.
What is Modern C++
What is modern C++? Put simply, C++ that is based on C++1x and uses modern best practices.
To really grasp the essence of Modern C++, read the Core Guidelines. But nobody does that right?
Talks & Books
I've also read parts of Effective Modern C++ by Scott Meyes and found it useful.
The talk takes a look at many of the new features in C++ and a couple of old features you may not have known about. With the goal of correctness in mind, Sean shows how to utilize these features to create simple, clear, and beautiful code.
TL;DR: No Raw Loops, No Raw Syntonization Primitives, No Raw Pointers.
<iframe src="https://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning/player" width="960" height="540" allowFullScreen frameBorder="0"></iframe>
Sean also gave another talk on the subject at Amazon's A9 Programming Converstaions course.
<iframe width="560" height="315" src="https://www.youtube.com/embed/IzNtM038JuI" frameborder="0" allowfullscreen></iframe> <iframe width="560" height="315" src="https://www.youtube.com/embed/vxv74Mjt9_0" frameborder="0" allowfullscreen></iframe>
Writing Modern C++
These are my do's and don'ts regarding modern c++. There are also other things I do in order to make sure my project's are written well:
- Use CMake to build your project
- Use memcheck to detect memory leaks
- Run fuzzers to validate input
- and more ...
[!] Are you using a package manager? please let me know.
Follow Well-Known Guidelines
First, follow C++ Core Guidlines. You don't need to actually read it, there are tools like
Clang-Tidy that have the core guidelines pre-baked. Once you get a warning please go ahead and read the whole guideline. It's important to understand Why the guideline exists.
Second, consider following well-known coding conventions and guidelines. On many occasions you can find tooling that help you follow guidelines created by big projects / corporations.
Clang-Format has pre-baked support for LLVM, Google, Chromium, Mozilla & WebKit.
Clang-Tidy has pre-baked checks that follow Boost, Google, LLVM and more.
Use Popular Libraries
Use standard / popular libraries as much as possible. I use:
I try to use the [Standard Library as much as possible.
Turn on warnings, and preferebly warnings as errors. I usually turn on
Werror. They are anoyying, but a neccessery evil IMO.
A few days ago I watched a talk called "Modernizing Legacy C++ Code" where they suggested to use RAII everywhere:
<iframe width="560" height="315" src="https://www.youtube.com/embed/LDxAgMe6D18?start=1641" frameborder="0" allowfullscreen></iframe>
I'm not suprised, I'm a huge fan of RAII. Not only it makes code cleaner, thus reducing bugs and memory leaks, it also has an extremely low performance impact (compared to golang's defer, which is used for the same purpose)
If Your'e interfacing C code, consider creating a scope guard. I use my home-baked defer-clone for that purpose.
On "Modernizing Legacy C++ Code" they also talked about using
const everywhere. At first it sounded weird, but it actually made a lot of sense once they showed a few examples:
<iframe width="560" height="315" src="https://www.youtube.com/embed/LDxAgMe6D18?start=2195" frameborder="0" allowfullscreen></iframe>
This is a rolling release. That is, I'll keep updating this post with new insights.