Orthodox C++ (2016)
21 points by prayerie
21 points by prayerie
The article really read as "just use C". There's several admonitions not to use certain C++ features, but not a single mention of a feature that is worthwhile. I'm not going to stop someone from writing C, but, if you want to write C, why not just write C?
I remember some older devs I knew who grew up with Fortran. They trained C developers to write C without interacting with the "mistakes" in the C language, like pointers and runtime memory allocation. After all, when was the last time you saw a CVE submitted for a C program that didn't have any pointers?
One of the linked positive examples is Dear ImGui. According to the FAQ of that project:
Dear ImGui uses a very small subset of C++11 features. In particular, function overloading and default parameters are used to make the API easier to use and code terser. Doing so I believe the API is sitting on a sweet spot and giving up on those features would make the API more cumbersome. Other features such as namespace, constructors, and templates (in the case of the ImVector<> class) are also relied on as a convenience.
This article is mostly pretty standard advice I'd give to people about C++.
Don’t use exceptions.
I understand the logic why people use exceptions, but I honestly probably couldn't even tell you how to throw one in C++, since over the years I never remember actually doing it. You need to follow exception guarantees if you use them, which leads to a lot of weird. You also end up with epilogues and a lot of .pdata. It's also viral, so if you write a library which uses exceptions, then my program now has to build for exceptions. I don't care if they're "minimum/no overhead on the happy path," I just don't like random hidden error dominoes in my programs.
Don’t use RTTI.
Most "modern" C++ is relatively flat hierarchies. RTTI is slow, and if slow was ok, you probably wouldn't be using C++.
Don’t use metaprogramming excessively
Metaprogramming is useful, but it's also possible to create massive compilation times and binary bloat from this.
Don’t use anything from STL that allocates memory,
If you want speed, you really need to control allocations and avoid or batch allocations on hot paths. You can use std:: but you gotta be careful it does inefficient things sometimes.
Standard lib header files are also like lead weights for compilation times. <algorithm> adds about a second of compilation to a translation unit on one of my projects.
Wary of any features introduced in current standard C++,
I wrote an entire project using std::format a few years ago which worked great on Windows with MSVC, on Linux with Clang, and GCC. Then tried to compile on my Mac, which uses a slightly different compiler/standard library and it wasn't supported.
I learned my lesson, and stay 1 C++ version "behind" on my personal projects.