Modern C++ transformed the language from C-with-classes into a safe, expressive systems language. Adopt these features incrementally.

C++11 Highlights

  • auto, range-for, lambdas, move semantics
  • nullptr, uniform initialization {}
  • Smart pointers, <thread>, <chrono>

optional and variant

  #include <optional>
#include <variant>

std::optional<int> parse(const std::string& s) {
    try { return std::stoi(s); }
    catch (...) { return std::nullopt; }
}

std::variant<int, std::string> v = 42;
std::visit([](auto& x) { std::cout << x; }, v);
  

Structured Bindings

  std::map<std::string, int> m;
for (const auto& [key, val] : m)
    std::cout << key << ": " << val;
  

constexpr

  constexpr int factorial(int n) {
    return n <= 1 ? 1 : n * factorial(n - 1);
}
static_assert(factorial(5) == 120);
  

Coroutines Preview

  #include <coroutine>
// C++20 coroutines enable async without callbacks
// See dedicated coroutines page
  

Common Pitfalls

  • Ignoring compiler or linter warnings until they become production bugs.
  • Skipping error handling on I/O, allocation, and network operations.
  • Using outdated patterns when modern idioms exist in your language version.
  • Testing only the happy path without edge cases and failure modes.

Best Practices

  • Write tests alongside implementation, not after.
  • Prefer explicit, readable code over clever one-liners.
  • Use the standard library before reaching for third-party dependencies.
  • Profile before optimizing; measure after.
  • Document public APIs and non-obvious invariants.

Memory and Performance Notes

Compile-time computation with constexpr reduces runtime cost.

Exercise

Rewrite a function returning -1 on error to return std::optional instead.

Hint: Use std::visit with overloaded lambdas for variant dispatch.

Real-World Application

Production codebases combine these fundamentals with logging, metrics, and error recovery. Study mature open-source projects in this language for idiomatic patterns.

Summary

Master this topic through hands-on practice before advancing to the next section in the learning path.

Debugging Checklist

  1. Reproduce with minimal input.
  2. Read error messages completely.
  3. Binary-search the problem space by commenting out code.
  4. Compare against a known-good reference implementation.
  5. Write a regression test once fixed.

Quick Reference

Review the code examples on this page and type them manually — muscle memory accelerates learning.

Further Reading

C++ Core Guidelines, cppreference.com, and Effective Modern C++ by Scott Meyers.

Real-World Context

These patterns appear in Chromium, Unreal Engine, PostgreSQL, and countless production systems.

Additional Examples

Consider how this topic applies in a larger project:

  // Break the problem into smaller functions
// Test each function independently
// Integrate incrementally
  

Working through variations of the examples above builds deeper understanding than reading alone.

Interview and Review Questions

  1. Explain the core concept of this topic in your own words.
  2. What happens when this code runs with edge-case input (empty, null, zero, max value)?
  3. How would you debug a bug related to this topic in production?
  4. What are the performance implications of the approach shown here?
  5. How does this feature compare to the equivalent in another language you know?

Review adjacent pages in the learning path before and after this one. Concepts build on each other — skipping ahead often leads to confusion when later pages assume mastery of earlier material.

Return to the section index if you need to fill gaps in prerequisite knowledge.

Tooling Tips

  • Enable all compiler or analyzer warnings during development.
  • Use version control with small, focused commits for each exercise.
  • Pair reading with typing — reproduce every code example by hand.
  • Run tests or compile after every change to catch errors early.
  • Keep a personal notes file linking concepts to your own project experiences.