DEV Community

Cover image for C++ Just Ain't What It Used To Be
steve poling
steve poling

Posted on

C++ Just Ain't What It Used To Be

An old man is wont to shake his cane and lament the decline of civilization's artifacts. I'm old, but I have no cane, nor is this a lament about the C++ programming language.

I was fluent in the C++ language having abandoned C moments after Bjarne Stroustrup invented the new shiny. I paid off my mortgage writing C++ programs, and paid for my Saturn with a Java program before moving to .NET/C# and toiling in that vineyard for a couple decades.

Retirement has brought a series of short term gigs that I've richly enjoyed. The most recent of which was an update of a legacy Windows application written in C++.

As I assessed the legacy codebase these things became immediately apparent:

  • this was a steaming pile of you-know-what,
  • its worst infractions were lapses into idiomatic C,
  • it was better than I would have written at the time.

The world has moved on from 80286 CPUs and many critical concerns of the 1990s are useless distractions today. Moreover, the C++ programming language is a lot different from the Borland Turbo C++ compiler that Jason Turner recently reviewed.

Since I returning to C++ after a long hiatus I've been watching a lot of Jason Turner's videos. What's struck me has been the dramatic changes from Borland C++ to C++20.

Just before I left C++ the STL and templates were a thrilling innovation. They were amazingly powerful until the slightest mistake prompted a wall of terrifying error messages to spill forth from the compiler. It was a simple matter of Pavlovian conditioning to make this programmer shy away from template meta-programming when deadlines loomed.

Two decades later everything has moved in this direction. And it appears that concepts in C++20 will make templates safe for normal people by making compiler error messages comprehensible.

I cringe at the thought of the hundreds of lines of code I wrote that can be readily replaced by three lines of today's standard library code.

I engaged the legacy C++ codebase by writing modern C++ everywhere I had to fix something or add something new (-std=c++17). At first I distrusted "auto almost everywhere" until I realized it didn't undermine strong typing. Now I happily pass around auto variables whose type I cannot articulate, but the compiler enforces their consistent usage.

Then there's lambdas. I fell in love with lambdas when I learned them in Python and rewrote all my C# loops as Linq expressions. When I started using lambdas in C++ STL algorithms, the legacy code became 10x more expressive. If I had been able to use C++20's ranged iterators, most of the legacy for loops would have been rewritten.

In the 1990s I did not trust the const keyword. I was an idiot. By simply changing most declarations to "const auto" I can reason about the code much more powerfully. Of course, modern compilers flag inconsistent usage.

Calling something const when I KNOW it's mutable gives rise to a flood of intentional error messages. "Oh, so that's where it's changing." This is a helpful way to get to know what a codebase is doing. I encourage temporarily injecting errors into code for this purpose.

The tooling makes it easy to do the right thing and harder to do wrong things. Tool tips in modern IDEs are a godsend. The structure of modern C++ makes it easier to reason about code.

What attracted me to C in the first place was "closeness to the metal" in the sense that I could look at C code and imagine the assembler the compiler would generate. But C had a big disadvantage: "if you want to cut your throat, C gladly hands you the knife." C++ brought many advantages to those who had been programming under the influence of Pascal.

Happily, the improvements of modern C++ need not come at the expense of efficiency. We can code up the brightest shiny new feature and drop it into something like Matt Godbolt's Compiler Explorer to see exactly what its assembler looks like.

What attracts me to modern C++ today is its ability to capture my intent in such an expressive way.

"You darned kids just don't appreciate how good you got it nowadays!" I said wagging my finger.

Discussion (2)

pgradot profile image
Pierre Gradot

It's very pleasant to read your article!

I have been seeing many comments lately about C++ getting too complicated: "auto is bad for typing", "templates are so hard to use", "there are too many features in the language", "I don't see the point in using lambdas"...

And what you say basically is: "older is not better: today's C++ is better" \o/

PS: this is the first time I see such an old IDE, I love CLion even more now!

steve_poling profile image
steve poling Author

Indeed, the language is improving. Those gainsaying autos, templates, & lambdas simply haven't wrestled with the problems motivating them.