2 In case you're doing minor changes in existing legacy code - use existing code style as you see in that code.
3 Otherwise follow guidelines below.
5 ## Indentation and alignment:
6 Code block indentation and alignment of hyphenated continuations - tabs.
7 Alignment of trailing comments - also tabs.
8 All other alignment in the middle of text line (if need) - spaces.
10 ## Spaces and parenthesis:
12 // No space between parenthesis and function name, no space in empty arguments list:
13 void FunctionWithoutArgs();
15 // Spaces between arguments, but no spaces between parenthesis and contained argument:
16 void ConstFunctionWithArgs(int i, std::string s) const;
18 // Spaces between expression elements and after if, no spaces between parenthesis and contained stuff:
19 if (condition1 && (condition2 || condition3)) {
22 // Space between try and opening brace, spaces around catch:
24 } catch (std::exception &) {
26 int arr[] = {1, 2, 3}; // - space both sides of equal sign here
27 int i{}; // - no space between variable and list initializer's brace
28 i = arr[1]; // - simple assignment - space surrounds both sides of equal sign
29 i+= arr[2]; // - incremental assignment - space only on the right
30 void *ptr_i = (void *)&i; // - pointer: space between target type and asterisk
31 int &ref_i = i; // - reference: space between target type and amperans
33 // Complex ternary expressions should have parenthesis to segregate things:
34 int ternary_result1 = simple_condition ? 1 : 2;
35 int ternary_result2 = (complex && condition) ? 1 : 2;
36 int ternary_result3 = simple_condition ? ((complex && condition) ? 1 : 2) : ((complex && condition) ? 3 : 4);
40 110 characters, buts its a soft limit, i.e. prefer better readability more than fiting limit.
41 Trailing comments are not affected by this limit at all.
44 Use direct (same line) braces for class-es/struct-s/union-s, functions, namespaces
45 Use egyptian braces for if-s, loop-s, try..catch-es, lambda-s
46 In case of if..elseif.. with long complex code within block - use empty line to accent end of code block.
47 In case code-block-start operator has short own condition and has as child single another code-block-starter, you may put that secondary operator at same line as first one and use single indentation for its code block.
48 In case of very short inlined class methods - you may write method definition's code block as single line.
49 In all other cases put any nested operator and its code block starting from separate line and with its own indentation level.
55 int Short() const { return _result; }
64 if (long && complicated && condition) {
102 ## Hyphenated continuations:
103 Indent second line of condition using two tabs to separate it from code block.
104 Put hyphenated operators on new line's beginning.
114 Use CamelCase for name of enums, namespaces, classes, structures, functions and methods
115 Use snake_case for all variables, however:
116 Private and protected class's fields - prefix by '\_'
117 Static variables - prefix by 's\_'
118 Global nonstatic variables - prefix by 'g\_'
119 Use UPPER_CASE_WITH_UNDERSCORES for macroses, values of enum-s.
120 Additionally values of enums must be prefixes with a abbreviation of corresponding enum's name.
122 For template arguments that represent type name - use CamelCaseT (camel case with T suffix).
123 For template arguments that typed constant value - use UPPER_CASE_WITH_UNDERSCORES_T.
124 If template function represents 'internal' implementation for some nearby non-templated functions - you may add T suffix for its name to clearly denote this.
140 void CallMe(SomeEnum argument)
142 static int s_static_var;
143 int local_var = argument + _private_field;
144 s_static_var = std::max(local_var, s_static_var);
149 template < class ClassT, int VALUE_T >
150 ClassT TemplateFunction(int arg)
152 return ClassT(arg + VALUE_T);
158 Use struct if your class exposes some public data fields or/and only public methods otherwise prefer using class.
160 ## class/struct layouts:
165 void PrivateMethod();
168 int _protected_field;
169 void ProtectedMethod();
184 Use CamelCase.cpp unless you're adding file into a directory that already have lots of files with different naming convention. In such case follow existing conventions.