This document is based on the work of
B. Weinberger, C. Silverstein,
G. Eitzmann, M. Mentovai
and T.Landray
at http://google-styleguide.googlecode.com,
C++ Google Style guide, Revision 3.274
under the CC-By 3.0 License
Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way:
. You may toggle all summaries with the big arrow button:As every C++ programmer knows, the language has many powerful features, but this power brings with it complexity, which in turn can make code more bug-prone and harder to read and maintain.
The goal of this guide is to manage this complexity by describing in detail the dos and don'ts of writing C++ code. These rules exist to keep the code base manageable while still allowing coders to use C++ language features productively.
Another issue this guide addresses is that of C++ feature bloat. C++ is a huge language with many advanced features. In some cases we constrain, or even ban, use of certain features. We do this to keep code simple and to avoid the various common errors and problems that these features can cause. This guide lists these features and explains why their use is restricted.
Note that this guide is not a C++ tutorial: we assume that the reader is familiar with the language.
In general, every .cxx
file should have an associated
.h
file. There are some common exceptions, such as
unittests
and small .cxx
files containing just a main()
function.
Correct use of header files can make a huge difference to the readability, size and performance of your code.
The following rules will guide you through the various pitfalls of using header files.
#define
guards to
prevent multiple inclusion. The format of the symbol name
should be
<PROJECT>_<PATH>_<FILE>_H_
.
Avoid using #pragma once
.
#include
s.
.cxx
file and let the compiler decide what gets inlined (it can decide anyway, regardless of the inline keyword).
Use inline when you require the implementation of a function in multiple translation units (e.g. template classes/functions).
Namespaces subdivide the global scope into distinct, named scopes, and thus are useful for logically grouping related types and functions and preventing name collisions.
.cxx
files, they are not allowed in .h
files.
.cxx
file,
inside the named namespace that wraps an entire .h
file and in functions and methods.
std
, not even forward
declarations of standard library classes.
struct
only for passive objects that carry data;
everything else is a class
.
public
and declare overriden methods
as override
or final
.
However, composition is often more appropriate than inheritance especially if a
class is not designed to be a base class.
const
.
Design const-correct interfaces.
Consider constexpr
for some uses of const.
constexpr
to define true constants or to ensure constant initialization.
std::unique_ptr
and std::shared_ptr
should be used consistently instead of non-owning raw pointers.
Never use owning raw pointers, and thus never use delete
.
The use of raw pointers may need an explanation in form of a comment.
static_cast
when necessary, but avoid const_cast
and reinterpret_cast
.
C-casts are forbidden.
++i
) and decrement
(--i
) operators because it has simpler semantics.
If not conditional on an enumerated value, switch statements
should always have a default
case.
Empty loop bodies should use {}
or continue
.
int
if you need an integer type.
Prefer signed integers over unsigned integers and thus std::int64_t
over unsigned int
if you need more bits.
auto
to avoid type names that are just clutter.
Continue to use manifest type declarations when it helps readability,
and never use auto
for anything but local variables.
The coding conventions described above have to be followed. However, like all good rules, these sometimes have exceptions.
Use common sense and BE CONSISTENT.
The point about having style guidelines is to have a common vocabulary of coding so people can concentrate on what the programmer is saying, rather than on how he/she is saying it.
OK, enough writing about writing code; the code itself is much more interesting. Have fun!
[1] Herb Sutter on software, hardware, and concurrency blog [http://herbsutter.com/2013/05/09/gotw-1-solution]