Source metadata
- Type: Textbook
- Author: Bjarne Stroustrup
- Publisher: Pearson / Addison-Wesley (Informit)
- Year: 2023 (C++20)
- Structure: 19 chapters + Appendix A. A guided survey of C++20 — not a tutorial but a thorough overview for programmers who already know at least one language.
Key takeaways
- C++ is a compiled, statically typed, value-semantic language. Unlike C# or Java, assignment copies values by default — sharing requires explicit pointers or references. Every entity’s type must be known to the compiler at its point of use.
- Pointers and references are distinct. A raw pointer (
T*) can be null and can be reassigned; a reference (T&) cannot be null, cannot be reseated after binding, and is implicitly dereferenced. Both map to hardware addresses. - RAII is the C++ resource model. “Resource Acquisition Is Initialization” — constructors acquire resources, destructors release them deterministically. This replaces garbage collection with scope-controlled resource management.
- Smart pointers replace naked
new/delete.unique_ptr<T>represents sole ownership (destructor deletes);shared_ptr<T>represents shared ownership (reference-counted delete). Prefermake_unique<T>()andmake_shared<T>()over rawnew. - Move semantics enable cheap transfer. Rvalue references (
T&&) and move constructors allow transferring ownership of resources without copying, resolving the performance cost of returning containers from functions. - Templates are a compile-time parameterisation mechanism. Unlike C# generics (which use runtime type erasure), C++ templates are instantiated at compile time for each concrete type — zero runtime overhead, but code bloat risk. Function templates, lambda expressions, and function objects are the key tools.
- Concepts (C++20) constrain templates. A concept is a compile-time predicate on a template argument (
template<Element T>), giving meaningful error messages instead of instantiation-time failures. structandclassdiffer only in default access. Astructhas public members by default; aclasshas private members by default. Both support constructors, member functions, and inheritance.enum classover plainenum. Scoped enumerations prevent name collisions and don’t implicitly convert to integers — safer and more explicit than C’s plain enums.- Modules (C++20) improve on headers. The traditional
#includesystem has compile-time and consistency problems;import(C++20) offers proper separate compilation. Both are shown and the headers approach remains common in older code.
Notable claims
“The technique of acquiring resources in a constructor and releasing them in a destructor, known as Resource Acquisition Is Initialization or RAII, allows us to eliminate ‘naked new operations’… Avoiding naked new and naked delete makes code far less error-prone and far easier to keep free of resource leaks.” — Ch. 5
“Templates are a compile-time mechanism, so their use incurs no run-time overhead compared to hand-crafted code.” — Ch. 7
“In C++, resource management is primarily delegated to a garbage collector. In C++, you can plug in a garbage collector. However, I consider garbage collection the last choice after cleaner, more general, and better localized alternatives to resource management have been exhausted.” — Ch. 6
“A reference must refer to a valid object… There is no ‘null reference.‘” — Ch. 1
Relevance
This source primarily informs:
- cpp-basics — fundamental C++ types, pointers, references, scope, constants, control flow
- cpp-classes-and-oop — structs, classes, constructors, virtual functions, class hierarchies, abstract types
- cpp-memory-management — RAII, destructors, unique_ptr, shared_ptr, move semantics
- cpp-templates — parameterized types, function templates, lambdas, concepts
It also provides comparative context for:
- csharp-variables-and-types — C++ value semantics vs C# reference semantics for classes
- csharp-oop-fundamentals — C++ class vs C# class; struct differences
- csharp-collections — STL containers vs C# List/Dictionary
Open questions raised
- C++ does not mandate a garbage collector; RAII and smart pointers are the alternative. For game developers coming from Unity/C#, the mental shift to deterministic scope-based destruction is significant. Where are the transition failure modes most common?
- The book presents C++20 features (modules, concepts, coroutines) as current. Unreal Engine uses an older C++ standard (primarily C++17 in UE5) — which features are available varies by engine version.
- Template instantiation at compile time can cause significant code bloat in large projects. Unreal Engine works around this partly through its reflection system (
UCLASS,UPROPERTY,UFUNCTIONmacros). How does the interaction between templates and Unreal’s macro system work in practice?