Learning Modern C++
C++ is the language of high-performance systems, game engines, and real-time applications. Master modern C++ (C++20/23) with focus on efficiency, safety, and scalable architecture.
Why Learn C++?
- Performance: Zero-cost abstractions and compile-time optimization. No GC overhead.
- Systems programming: Direct hardware access for OS, embedded, and kernels.
- Game development: Unreal Engine, Godot, and most AAA engines use C++.
- Real-time apps: Financial systems, robotics, and low-latency trading.
Modern C++ vs Legacy C++
| Feature | Modern C++ (C++20/23) | Legacy C++ | Benefit |
|---|---|---|---|
| Memory Management | Smart pointers (unique_ptr, shared_ptr) | Manual new/delete | Memory safety |
| Loops | Range-based for (for-each) | Index-based loops | Cleaner, safer |
| Type Deduction | auto keyword | Explicit types everywhere | Less boilerplate |
| Generics | Concepts (C++20) | SFINAE tricks | Better errors |
Your Learning Path
Follow this structured progression from basics to advanced modern C++.
variables, types
heap, stack, RAII
inheritance, polymorphism
vectors, maps, algorithms
unique_ptr, shared_ptr
concepts, metaprogramming
rvalues, perfect forwarding
threads, mutexes, atomics
Interactive Lessons
Expand each lesson to learn. Try examples with Compiler Explorer to see generated code.
Variables, Types, and RAII
Modern C++ emphasizes automatic memory management through RAII (Resource Acquisition Is Initialization). Variables should be initialized properly to avoid undefined behavior.
#include
#include
int main() {
// Stack variables (automatic lifetime)
int x = 42;
double pi = 3.14159;
// Smart pointers (RAII - automatic cleanup)
std::unique_ptr ptr(new int(100));
std::shared_ptr str = std::make_shared("hello");
// String (manages memory automatically)
std::string message = "Modern C++";
// No manual delete needed!
return 0;
}
Key Point: Never use raw pointers for ownership. Use unique_ptr for exclusive ownership, shared_ptr for shared ownership.
Pointers, References, and Move Semantics
Move semantics enable efficient transfer of resources. Understanding lvalues vs rvalues unlocks performance gains.
class Buffer {
std::vector data;
public:
// Move constructor (steal resources)
Buffer(Buffer&& other) noexcept
: data(std::move(other.data)) {}
// Move assignment
Buffer& operator=(Buffer&& other) noexcept {
data = std::move(other.data);
return *this;
}
};
Buffer create_buffer() {
Buffer b;
return b; // Calls move constructor (cheap!)
}
Key Point: Move semantics avoid expensive copies. Compiler uses RVO/NRVO when possible.
Classes, Inheritance, and Polymorphism
OOP in C++ uses virtual functions for runtime polymorphism. Prefer composition over deep inheritance hierarchies.
class Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
virtual void draw() const = 0;
};
class Circle : public Shape {
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return 3.14159 * radius * radius;
}
void draw() const override {
std::cout << "Drawing circle\n";
}
};
// Usage
std::unique_ptr shape = std::make_unique(5.0);
std::cout << shape->area() << "\n";
shape->draw();
Key Point: Mark overrides with override keyword. Use virtual and = 0 for pure virtual methods.
Templates and Concepts (C++20)
Templates provide compile-time polymorphism. C++20 Concepts make template errors readable.
#include
// Concept: anything printable
template
concept Printable = requires(T t, std::ostream& os) {
{ os << t } -> std::convertible_to;
};
// Generic function using concept
template
void print_all(const std::vector& items) {
for (const auto& item : items) {
std::cout << item << "\n";
}
}
int main() {
std::vector nums = {1, 2, 3};
print_all(nums); // Works!
struct Foo {};
std::vector foos;
print_all(foos); // Compile error: Foo is not Printable
}
Key Point: Concepts enable generic programming with clear constraints.
STL Containers and Algorithms
STL provides optimized data structures and algorithms. Choose the right container for your use case.
#include
#include
#include
int main() {
// Vector: contiguous, fast random access
std::vector nums = {3, 1, 4, 1, 5};
std::sort(nums.begin(), nums.end());
// Map: ordered key-value pairs
std::unordered_map counts;
counts["apple"]++;
counts["banana"]++;
// Algorithms with lambdas
auto sum = 0;
std::for_each(nums.begin(), nums.end(),
[&sum](int n) { sum += n; });
std::cout << "Sum: " << sum << "\n";
}
Key Point: Use range-based for loops and STL algorithms for safer, cleaner code.
Concurrency: Threads and Synchronization
Modern C++ provides std::thread for concurrent programming. Use mutexes and atomics for thread-safe access.
#include
#include
std::mutex mtx;
int shared_value = 0;
void increment() {
for (int i = 0; i < 1000; ++i) {
std::lock_guard lock(mtx);
shared_value++;
}
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << "Final value: " << shared_value << "\n";
}
Key Point: Use std::lock_guard for RAII-based mutex management. Avoid manual lock/unlock.
Curated GitHub Repositories
Explore hand-picked C++ repositories showcasing real-world architecture and patterns. Filter by category or search.
Practice Projects
Build expertise with these modern C++ projects:
-
1. Smart Pointer Memory Manager
Create a custom allocator using unique_ptr and shared_ptr. Practice RAII, move semantics, and resource ownership.
Key Concepts: unique_ptr, shared_ptr, RAII, move constructors -
2. Thread Pool Implementation
Build a task queue with worker threads. Practice std::thread, mutex, condition_variable, and lock-free synchronization.
Key Concepts: std::thread, std::mutex, std::condition_variable, task queues -
3. Entity-Component-System (ECS)
Implement a game-ready ECS architecture. Practice templates, inheritance, and data-oriented design patterns.
Key Concepts: ECS patterns, templates, polymorphism, composition -
4. Template Metaprogramming Calculator
Build a compile-time expression evaluator using template specialization. Practice SFINAE and Concepts.
Key Concepts: Templates, SFINAE, Concepts, compile-time computation -
5. OpenGL Renderer
Build a modern OpenGL renderer with resource management, shaders, and a scene graph. Apply all modern C++ patterns.
Key Concepts: Smart pointers, move semantics, design patterns, graphics API
Resources Hub
Learning Resources
Modern C++ Guides
Tools & Compilers
Community & Events
Your Progress
Check off lessons as you complete them. Your progress is saved locally in your browser.