Programming in C++, the Evolution of C++11, C++14, C++17, C++20 and C++23

Cobalt Traffic Controller

Econolite Cobalt traffic light controller, safety-critical real-time embedded Linux system with 7″ TFT touch screen, C++ and Cmake

HOLLYWOOD, CA (GoshRobin) 2022/6/22 – Someone asked me, Robin, what are the differences between the standard versions of C++?

What’s New in C++11

Being from 2011, C++11 is over ten years old at this point. Anyone using an older version of C++ is way out of date. There’s now compiler support for C++14, C++17, C++20, and even some features of C++23.

C++11 heap management with unique_ptr is vastly better than the old way of calling delete explicitly. A modern C++ program design never needs to call delete. Using unique_ptr is an awesome coding improvement because it automatically frees memory and is much more efficient that using Garbage Collection (GC)

Using shared_ptr is more of hack, is the C++ concept of having GC that memory frees automatically when nothing points to it. I led a casino game server project where I used shared_ptr to quickly clean up C++ game server code I’d ported from Java. That not-made-for-C++ code leaked everywhere due to missing C++ delete calls. Although Java syntax looks very similar to C++, Java has no delete operator. Java programmers don’t explicitly free memory. GC is automatic. Many prefer Java GC as a convenient way to avoid the notorious C/C++ memory leaks caused by sloppy programmers forgetting to deallocate memory they had allocated. However, the performance cost of GC can be high. Java’s mark-and-sweep GC has a global lock, will stop the world while it moves memory around.

Running a 3D game server, GC may produce a noticeable pause that happens at random when the GC decides to run, freezing all players for maybe seconds depending upon how much memory the Java runtime has to move. Random freezes infuriate game players. For this reason, Java may not be the best choice for a First-Person-Shooter (FPS) Virtual Reality (VR) game. Microsoft Windows also has some global memory locks at the OS level that can stop the world. For performance reasons, migrating to C++ on Linux was chosen over staying with Java on Windows, but it was an ambitious undertaking.

My Java-to-C++ game server migration was much harder and took longer than expected. The legacy Java code used one thread per game table, was not scalable, and had lots of bugs and bad practices. The Java code thrashed memory, that when it found it didn’t have enough memory to seat players, it would reallocate memory on the heap by doubling whatever amount of memory the table was using. It would do this repeatedly. I made the  memory allocation sane, turned the game logic inside-out to make it be asynchronous with thread pooling, fixed countless bugs, and added bingo and lotto games to our poker game server.

Using auto type detection with the STL calls makes using containers easier. The range-based for() loop of C++11 is significant. It’s just a little nicer syntax, but gets used a lot. Lambda Functions are very useful with threads and in STL algorithm calls. I always use Named Lambda Functions. While many programmers don’t bother, I consider function names key to having self-documented code.

Deleted functions are useful for removing the default copy and assignment operators that exist in C++ for backward compatibility with C code. Those defaults can lead to surprises in C++ code where the designer didn’t intend for objects to implicitly copy. Using move semantics with std::move is a nice copy optimization feature.

C++11 added the unordered containers from TR1, Rvalue references, nullptr, the STL iota algorithm, a lot of stuff, but the new features I already mentioned are the game-changers in my experience. Thread promises and co-routines seem interesting, but I’ve not encountered them in production code. When I worked on the development of the Econolite Cobalt traffic controller we had a programmer in love with constexpr for initialization optimization.

What’s New in C++14, C++17, C++20, and C++23

The std::chrono library introduced in C++11 and improved in C++14 is useful for handling dates. C++14 added generic lambda and auto return type, minor improvements to features added in C++11.

C++17 introduced the std::filesystem library, replacing the legacy C library.

C++20 has calendar and timezone library improvements. It also adds the spaceship less-than/greater-than operator <=>.

C++23 adds the multidimensional subscript operator and string::contains().

C++ Creator Bjarne Stroustrup

Bjarne Stroutrup

Bjarne Stroutrup

I’m grateful to Bjarne Stroustrup, the creator of C++,and to friends on the C++ standards committee who have made C++ what it is, the most successful programming language in the world. As an instructor at the University of Washington, I used Stroustrup’s book Programming in C++ as our textbook for the Intermediate C++ course I taught. As a professor at the Naval Postgraduate School, I used Stroustrup’s book Advanced C++ as our textbook for the Advanced C++ course I taught.

Bjarne and I have known each other since 1995, when I first taught at university using his book. I have been designing code in C++ even longer than that. If you’re interested in the philosophy of C++, read Bjarne’s I Wrote C++ Primarily for Myself and My Colleagues. Bjarne works at Morgan Stanley.



About Robin Rowe

Robin RoweRobin is a