projects /

C++ Default Guidelines

An opinionated set of default guidelines for writing better, simpler C++.

  • C++
  • Guidelines
  • MIT

Summary

A compact set of 18 core guidelines, such as turning on compiler warnings, avoiding raw loops, managing resources with RAII and avoiding owning raw pointers. Each one comes with an explanation and references to the C++ Core Guidelines and to the teachings of experts like Scott Meyers, Sean Parent and Jason Turner.

Purpose

Big guideline collections are comprehensive, but they can be overwhelming. Nobody wants to read a 200-page document before they start coding, and nobody can keep all of it in mind while they work. The Default Guidelines are deliberately short: a list you can memorize, with more detail available when you want it.

They are also deliberately opinionated. Where other sources say “it depends”, the Default Guidelines still give an answer. Of course it depends, but having a go-to default is valuable, and the more often we all choose the same defaults, the less surprising our code becomes.

Defaults, not rules

The Default Guidelines are guidelines, not rules. A rule says “do this or else”. A default says “do this, unless you have a reason not to”.

Kate Gregory put it well: a guideline is like a fence along a river. It keeps you from falling in by accident, but if you want to go into the river, you can. Use the guidelines as the default, and if you go beyond them, tread carefully.

Take “do not use reinterpret_cast”. I can hear you think “but, sometimes…”. Exactly: sometimes you need it. That is the point of a default. You follow it when there is no reason to do otherwise, and you deviate when there is one.

Why default guidelines, rather than company or team guidelines

Most companies and many teams have their own coding guidelines. They are usually written as rules, they grow longer over time, and they mix style (spaces, braces, names) with structure (ownership, casts, loops). Every company writes its own, so every new colleague has to learn a new rule book, and code from one team reads differently from code of the team next door. Rules that are too strict get ignored, and then the document stops meaning anything at all.

Default guidelines work differently, because they are the choice you make when there is no reason to choose otherwise:

  • They make code unsurprising. When the same problem is solved the same way everywhere, a reader recognizes the pattern and can move on. The reader’s attention goes to the code that matters.
  • They make code consistent across teams and projects. The defaults are not tied to one company, so they travel with you. A new colleague who knows the defaults can read your code on day one.
  • They make deviations stand out. Code that follows the defaults is boring in the best sense of the word. When something deviates, that is a signal: this code is special, and it deserves a closer look. Under a long rule book, the important exceptions disappear among the trivial ones.
  • They stay small. A short list of defaults can be remembered and applied while writing code, not only looked up in a review.

So instead of writing your own rule book, start from the Default Guidelines and write down only where your team deliberately deviates, each with its reason. For example, the detailed guidelines recommend #pragma once, but a team that needs strict standard compliance may decide to use include guards instead, and write down why. Such a list of deviations is short, it explains itself, and everything that is not on it follows the defaults.

Comply or explain

The motto I recommend is comply or explain. It is not meant as an order, just as an easy way to remember the idea: if you choose a non-default solution, you should be able to say why.

Think it through for yourself first, and then put the explanation in the code, for the next reader. Move the unusual code into a function whose name says what it does, and add a comment that explains why it is needed (see I.30 in the C++ Core Guidelines). If you can explain why you deviated, you have shown that you thought about it and concluded that something else was better. And that is great.

The guidelines should never be followed blindly either. Think about the code, apply the defaults, and if they work well, do not complicate things any further.

Guidelines and style

The Default Guidelines are about structure and language constructs, not about style. For style, meaning whitespace, braces and naming, the most important thing is to have one consistent style, rather than which style you choose. Pick one, enforce it with a tool like clang-format, and do not apply it to third-party code. The repository includes example .clang-format and .editorconfig files.

The guidelines

  1. Turn on warnings, and use sanitizers.
  2. No raw loops.
  3. Avoid global mutable state.
  4. Keep the scope of variables and type declarations as limited as possible.
  5. Initialize all variables at declaration.
  6. Use constexpr or const whenever you can (but not for member variables and return types).
  7. Use [[nodiscard]] on const member functions that return a value.
  8. Avoid returning values through arguments.
  9. Use automatic resource management (RAII).
  10. Follow the rule of 0, or else the rule of 5.
  11. Avoid owning raw pointers.
  12. Avoid manual memory management with new, delete, malloc, free, etc.
  13. Do not use C-style casts.
  14. Do not add member variables to classes used as interfaces.
  15. Do not use protected member variables.
  16. Avoid volatile, const_cast, reinterpret_cast, typedef, register, extern, protected and va_arg.
  17. Make the destructors of classes used in runtime polymorphism virtual.
  18. Avoid references as data members of a class.

The README links each guideline to the C++ Core Guidelines, and the detailed guidelines give the rationale for each one.

Key features

  • 18 core guidelines with rationale and references
  • A beginner’s guide and a refactoring guide
  • Examples and compiler warning recommendations
  • Ready-to-use .clang-format and .editorconfig files
  • The slides of my C++ Under the Sea 2025 talk about the Default Guidelines

GitHub page

Technology

Markdown documentation plus configuration files, MIT licensed. One of the most-starred repositories on my GitHub.