C++ Core Guidelines¶
The C++ code guidelines are a set of rules to use for modern C++ projects created by the C++ community.
In the following section, we will list up the entries which are of considerable interest for this project.
There will be some gaps in the number as we skipped some of the less importer ones.
Also the code guidelines have gaps by default (blank space for new rules).
Philosophy¶
Interfaces¶
Functions and class methods¶
F.1: “Package” meaningful operations as carefully named functions
F.4: If a function may have to be evaluated at compile time, declare it constexpr
F.7: For general use, take T* or T& arguments rather than smart pointers
F.15: Prefer simple and conventional ways of passing information
F.16: For “in” parameters, pass cheaply-copied types by value and others by reference to const
F.18: For “will-move-from” parameters, pass by X&& and std::move the parameter
F.20: For “out” output values, prefer return values to output parameters
F.21: To return multiple “out” values, prefer returning a struct or tuple
F.26: Use a unique_ptr<T> to transfer ownership where a pointer is needed
F.43: Never (directly or indirectly) return a pointer or a reference to a local object
F.51: Where there is a choice, prefer default arguments over overloading
Classes¶
C.2: Use class if the class has an invariant; use struct if the data members can vary independently
C.3: Represent the distinction between an interface and an implementation using a class
C.4: Make a function a member only if it needs direct access to the representation of a class
C.7: Don’t define a class or enum and declare a variable of its type in the same statement
C.8: Use class rather than struct if any member is non-public
Enumerations¶
Resource management¶
R.2: In interfaces, use raw pointers to denote individual objects (only)
R.5: Prefer scoped objects, don’t heap-allocate unnecessarily
R.12: Immediately give the result of an explicit resource allocation to a manager object
R.13: Perform at most one explicit resource allocation in a single expression statement
Classes¶
C.30: Define a destructor if a class needs an explicit action at object destruction
C.31: All resources acquired by a class must be released by the class’s destructor
C.35: A base class destructor should be either public and virtual, or protected and non-virtual
C.41: A constructor should create a fully initialized object
C.42: If a constructor cannot construct a valid object, throw an exception
C.43: Ensure that a copyable (value type) class has a default constructor
C.44: Prefer default constructors to be simple and non-throwing
C.46: By default, declare single-argument constructors explicit
C.47: Define and initialize member variables in the order of member declaration
C.64: A move operation should move and leave its source in a valid state
C.80: Use =default if you have to be explicit about using the default semantics
C.81: Use =delete when you want to disable default behavior (without wanting an alternative)
C.82: Don’t call virtual functions in constructors and destructors
C.90: Rely on constructors and assignment operators, not memset and memcpy