Post

C++ styleguide

My personal preferred C++ style.

C++ styleguide

General formatting

  • Indentation: 4 spaces, no tabs
  • Braces: Allman style
  • Line length: ≤ 100 characters
  • Pointer alignment: Right (int* x)
  • Spaces:
    • One space after keywords (if, for)
    • No space in function call parentheses
    • Spaces around binary operators
1
2
3
4
if (x == 10)
{
    move(x + y);
}

Naming conventions

EntityStyleExample
ClassUpperCamelCasePlayerCharacter
StructUpperCamelCasePlayerStats
Private memberlowerCamelCase + _health_
Public memberlowerCamelCasescore
FunctionslowerCamelCaseupdatePosition()
VariableslowerCamelCaseplayerCount
ConstantskCamelCasekMaxLives
EnumUpperCamelCaseWeaponType
Enum valuesUPPERCASESWORD
NamespaceUpperCamelCaseSWORD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
BasedOnStyle: LLVM

### Indentation
IndentWidth: 4
TabWidth: 4
UseTab: Never
ContinuationIndentWidth: 4

### Braces — Allman Style
BreakBeforeBraces: Allman

BraceWrapping:
  AfterClass: true
  AfterControlStatement: true
  AfterEnum: true
  AfterFunction: true
  AfterNamespace: true
  AfterStruct: true
  AfterUnion: true
  BeforeCatch: true
  BeforeElse: true
  SplitEmptyFunction: false
  SplitEmptyRecord: false
  SplitEmptyNamespace: false

AllowShortFunctionsOnASingleLine: Empty
AllowShortBlocksOnASingleLine: Never
AllowShortIfStatementsOnASingleLine: Never

### Line Length
ColumnLimit: 100

### Pointers & References
PointerAlignment: Right   # int* x

### Spaces
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
#SpacesInParens: false #not supported in older clang
SpacesInCStyleCastParentheses: false
SpacesInAngles: false

### Alignments
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignOperands: true
AlignTrailingComments: true

### Includes
SortIncludes: true
IncludeBlocks: Regroup

### Comments
ReflowComments: true

This post is licensed under CC BY 4.0 by the author.

Trending Tags