Repository navigation
Cplusplus Style Guide
Amittai Aviram edited this page Dec 17, 2025
·
2 revisions
- Header files. File names end in
h. If the file contains a class definition, it should contain only one principle class definition, but may contain closely-related structure type or class definitions. The name of the file is the name of the principal type defined therein, which should be in UpperCamelCase:class Personis defined inPerson.h. If a header file contains other global data, not a type definition, its name should be simple and in lower case:constants.hfor symbolic constant definitions. All such files should be placed in the top-level directoryheader. - Implementation (source code) files. Every type definition header file should hav ea corresponding source code file, with the same name, also in UpperCamelCase, but the
.cppfile name extension. All functions should be defined out of line, and the out-of-line definitions should be placed in the appropriate source code files. All.cppsource code files should be placed in thesrctop-level directory.main.cppcontains themainfunction and belongs in thesrcdirectory. - Makefile. A project-level makefile should appear as the top level and should be named
makefile. Subdirectories may contain makefiles temporarily for development purposes, but they must eventually be removed.
- Avoid long lines. Limit lines to 80 characters. Use
:set cc=80to have Vi draw a vertical line at column 80. (Use:set cc=to make the vertical line disappear.) - Avoid comments. Use self-explanatory names and clear logic instead.
- Avoid long functions. If you feel the need for comments or vertical spaces within a function, your function is probably too long and complicated. Each function should "do only one thing and do it well."
The following sections must be separated from each other by single vertical spaces.
-
#pragma oncedeclaration (header only). -
#includedirectives for system (C++ library) headers, in alphabetical order. Include only the headers that the compiler needs for the symbols in the current file! -
#includedirectives for project headers, in alphabetical order. - Symbolic constants defined through the
#definedirective. These should be avoided, except (perhaps) for those used for conditional compilation. Symbolic constants, in general, should be global ``variables'' qualifed asconstand defined in `header/constants.h`. Include only the necessary headers! - Type definitions. Accessory type definitions (such as structure type definitions of types used in class defiinitions) should come first.
- Within a class definition, the
publicsection comes before theprivatesection. Thepublicsection should contain public (API) functions only, with constructors and destructor coming first:- Default constructor (if any)
- Non-default constructors (if any)
- Copy constructor (if any)
- Destructor (if any)
- Other API functions (if any).
- Enumeration types, wherever possible, should be associated with specific classes and should be defined within their respective class definitions. Typically, an enumeration type belongs in the
publicsection. - The
privatesection should list functions above fields. - Structure types should not use access specifiers at all — they should only be used for cases where everything is
public(by default), and should be used for types that serve primarily as mere data containers.
- Within a class definition, the
- Function definitions. Overloaded functions should occur in sequence, with overloads using smaller (or empty) parameter lists coming first.
- Custom-defined types (class, structure, union, and enumeration types) are named in UpperCamelCase:
Person,PublicSector. - Functions (other than constructors and destructors), global and local variables (including function parameters), and local constants (but not global constants) are in lower_snake_case.
- Enumerators in enumeration types and global constants (defined in
constants.h) are in ALL_CAPS_SNAKE_CASE.
- Four spaces for every indentation level. Do not use tab characters!
- One space before and after the
*used as part of a type designation in a declaration:Person * person. - One space before and after a binary arithmetic, relational, or logical operator and before and after the assignment operator:
-,+,-,*,/,%,==,!=,<,<=,==,!=,>=,>,&&,||,<<,>>,&,| - No space around a member access operator:
person.get_age(),person->get_age() - No space between a unary operator and its operand: -
*deref,!value,++value,value++,--value,value--,~bit_flip.
- No vertical spaces within a function body. If you are tempted to insert a vertical space in a function body, you should probably break the function up into smaller functions.