C++ Reference ยท Preprocessor, Tools and Concepts
The C++ Core Guidelines
A practical summary of the C++ Core Guidelines: ownership, RAII, const, interfaces, bounds safety and error handling, with before-and-after examples.
The C++ Core Guidelines are a large, community-maintained set of recommendations for writing modern C++, led by Bjarne Stroustrup and Herb Sutter. Their goal is code that is type-safe, resource-safe and free of avoidable undefined behavior, with no loss of speed. They are guidelines, not law: each rule has a reason, so you can judge when an exception is sound.
Rules have short IDs: P (philosophy), I (interfaces), F (functions), C (classes), R (resource management), ES (expressions and statements), E (error handling), T (templates), CP (concurrency), SL (standard library) and Per (performance). This page covers the habits that prevent the most bugs, with examples that run.
// R.20, R.11: own resources with smart pointers, never raw new/delete auto widget = std::make_unique<Widget>();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
The rules that matter most
Resources and ownership
- R.1, R.11: every resource has an owner that releases it automatically (RAII). Avoid naked
newanddelete. - R.20, R.21, R.22: use
std::unique_ptrfor sole ownership andstd::shared_ptronly when ownership is really shared; create them withmake_uniqueandmake_shared. - R.3, F.7: a raw pointer or reference means non-owning. Functions that only use an object take
T&orT*, not a smart pointer. - C.20 (rule of zero): if members manage themselves, write none of the five special member functions. If you write one, consider all five (C.21).
Interfaces and functions
- I.1, I.4: make interfaces explicit and strongly typed. Prefer
Distanceorstd::chrono::secondsto a baredoubleorint. - F.16: pass cheap types (about two words or less) by value; pass larger read-only objects by
const&. - F.20, F.21: return values (structs,
std::pair, structured bindings) instead of output parameters. - F.4, F.6: use
constexprwhere possible, andnoexceptfor functions that cannot throw. - I.13, SL.con.3: pass arrays as
std::span, never as pointer plus length.
Expressions and safety
- Con.1, Con.2: make objects and member functions
constby default. - ES.20, ES.22: always initialize; declare variables as late as possible, in the smallest scope.
- ES.46, ES.49: avoid narrowing conversions; if you must cast, use a named C++ cast (
static_cast), never a C-style cast. - ES.100-107: do not mix signed and unsigned arithmetic, and do not use unsigned types just to say "not negative".
- Bounds.1-4: avoid pointer arithmetic and array decay; use containers,
spanand range-for.
Classes and templates
- C.2, C.8: use
classwhen there is an invariant, andstructfor plain data with public members. - C.35, C.67: a base class destructor is either public and virtual or protected and non-virtual; polymorphic classes should suppress public copying, which prevents slicing.
- C.128, C.139: use
override, and mark leaf classesfinalif you do not design for inheritance. - T.10, T.11: constrain templates with concepts instead of unconstrained
typename T.
Errors and concurrency
- E.2, E.3: throw an exception to signal that a function cannot do its job; use exceptions for errors, not for ordinary control flow.
- E.12: use
noexceptwhen you cannot or do not want to recover (move constructors,swap, destructors). - CP.20, CP.21: use RAII locks (
std::scoped_lock) and never lock manually; lock several mutexes at once with onescoped_lock. - CP.2: avoid data races. Share as little as possible, and prefer immutable data.
- CP.25: prefer
std::jthreadtostd::thread.
Support libraries and tools
The Guidelines Support Library (GSL) supplies helpers such as gsl::not_null<T*>, gsl::owner<T*>, gsl::narrow<T> (a checked cast) and gsl::finally. Much of it has moved into the standard (std::span, std::unique_ptr, std::bit_cast).
Many rules can be checked automatically: the clang-tidy cppcoreguidelines-* checks, the Visual Studio C++ Core Check analyzer, and compiler warnings such as -Wshadow, -Wconversion and -Wold-style-cast. See Static analysis.
Usage notes
- Read the reason first. Each rule explains why. If the reason does not apply to your situation, the rule probably does not either, and you should document the exception.
- Adopt gradually. Start with the rules a tool can enforce (
override,nullptr, no nakednew, no C-style casts), then add ownership and interface rules as you touch code. - Do not turn it into dogma. The Guidelines favor safety, but sometimes performance or an external API requires a raw pointer or a cast. Keep such code small and well labeled.
- Prefer the standard library. Most guidelines are easier to follow with
std::string,std::vector,std::span,std::optionaland smart pointers than with hand-written equivalents. - Style rules differ from the Guidelines. Naming and formatting are not covered in depth (rule NL says to pick a convention and be consistent). See Naming and formatting.