Here’s a bird’s-eye comparison of “old-school” C++ (~2005–2008 era) vs. Modern C++ (C++11 through C++20+) and what you’d do “by the book” today.
1. Core Language Features
| Feature | 15 yrs ago (C++03) | Today (C++17/C++20+) |
|---|
| Type inference | None; you spelled out every type | auto for variables, return type deduction |
| nullptr | Used 0 or NULL for pointers | nullptr introduces a type‐safe null pointer |
| Enumerations | unscoped enum (implicit conversion to int) | enum class / enum struct (strongly scoped) |
| Constants | #define PI 3.14 or static const double | constexpr for compile-time constants & functions |
| Function objects | Functors (structs with operator()) or C‐style | Lambdas ([](int x){ return x*2; }) |
| Templates | SFINAE hacks, Boost.MPL | auto & template parameter deduction, Concepts (C++20) |
| Move semantics | None; everything was copied | Move constructors/assignments, std::move |
| Threading | boost::thread or OS APIs | <thread>, <mutex>, <future>, <atomic> in STL |
| Modules | N/A (huge header-include chains) | C++20 modules help speed up and encapsulate headers |
2. Resource Management & Ownership
Old style you’d write:
cpp
// C++03 style
MyType* p = new MyType;
// … maybe forget to delete p …
delete p;
Today you’d say:
cpp
// Modern C++
auto p = std::make_unique<MyType>(); // unique ownership
auto shared = std::make_shared<MyType>(); // shared ownership
Why?
- RAII & smart pointers eliminate most memory leaks.
- Raw
new/delete → rarely used except in very low-level code.
3. Algorithms over Hand-rolled Loops
Then:
cpp
std::vector<int> v = …;
for (size_t i = 0; i < v.size(); ++i) {
if (v[i] % 2 == 0)
v[i] *= 2;
}
Now:
cpp
#include <algorithm>
#include <ranges>
for (auto& x : v | std::views::filter([](int n){ return n % 2 == 0; }))
x *= 2;
Or simply:
cpp
std::ranges::for_each(v, [](int& x){
if (x % 2 == 0) x *= 2;
});
- Range-based for and std::ranges make intent clearer and less error-prone.
- Leverage
<algorithm> / <numeric> / <ranges> whenever possible.
4. Function Signatures & Safety
C++03:
cpp
int find(const std::vector<int>& v, int key);
Modern:
cpp
std::optional<size_t> find(const std::vector<int>& v, int key);
- Use
std::optional, std::variant, std::any instead of “magic return codes.”
- Mark non-throwing functions
noexcept.
- Use
= delete to forbid unwanted overloads (e.g. copy on noncopyable classes).
- Use
override & final on virtual methods to catch mistakes.
5. Constexpr & Compile-Time Computation
Then: almost no compile-time evaluation.
cpp
int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n - 1)); }
Now:
cpp
constexpr int factorial(int n) {
return n <= 1 ? 1 : (n * factorial(n - 1));
}
static_assert(factorial(5) == 120);
constexpr lets you push more logic into compile time.
- Better performance, earlier error detection.
6. Initialization & Uniform Syntax
C++03:
cpp
std::vector<int> v(3, 42);
Foo f = Foo(1, 2, 3);
Modern C++:
cpp
std::vector<int> v{1,2,3,4}; // initializer_list
Foo f{1,2,3};
Bar b{}; // zero-init
int x{}; // value-initialized to 0
- Uniform
{} syntax avoids narrowing conversions and “most vexing parse.”
7. Style & Guidelines
Then:
- Heavy use of macros (
#define)
- Prefixing member variables with
m_
- CRAP: copy-paste code, hand-rolled containers, home-grown smart pointers
Today:
- Follow the C++ Core Guidelines (Bjarne, Herb Sutter et al.)
- Use clang-format or clang-tidy for consistent style
- Prefer snake_case or lowerCamelCase consistently
- Minimize macros; prefer
constexpr, inline functions, and templates
Summary Checklist for “Right” Modern C++
- Use smart pointers & RAII; avoid naked
new/delete.
- Favor standard algorithms, ranges,
<functional> over manual loops.
- Leverage auto, structured bindings, and template argument deduction.
- Declare functions
constexpr/noexcept as appropriate.
- Adopt
enum class, nullptr, override, delete, final.
- Embrace the STL, avoid reinventing containers or strings.
- Stick to a style guide and automate formatting.
By embracing these modern idioms you’ll write safer, clearer, faster-to-compile, and more maintainable C++ code. 🚀