-
Notifications
You must be signed in to change notification settings - Fork 12
Formatting Guidelines
Finalspace edited this page Jul 5, 2025
·
12 revisions
Please use the following guidelines for formatting C/C++ code when contributing to any of my "final" projects.
All those are based on Visual Studio 2022 formatting rules for C/C++.
Therefore the structure of this guide matches exactly the C/C++ code style of Visual Studio (Options -> Text Editor > C/C++ > Code Style).
- Generated documentation comments style
- None
- XML Doc Comments
- Doxygen (///)
- Doxygen (/**)
- Doxygen (/*!)
- Doxygen (//!)
- Insert existing comment style at the start of new lines when writing comments
- Continue single line comments
- Automatically indent when i type a tab
- Automatically format statement when i type a;
- Automatically format block when i type a }
- When i paste
- Indent and format
- Indent, but not format
- Do nothing
- Automatically format braces when they are automatically completed
- Enable ClangFormat support
- ClangFormat execution
- Run ClangFormat for all formatting scenarios
- Run ClangFormat only for manually invoked formatting commands
- Default formatting style
- LLVM
- Chromium
- Mozilla
- WebKit
- Visual Studio
- Indent braces
class MyClass {
int Function() {
return 0;
}
}
- Indent each line relative to
- Outermost parenthesis
- Innermost parenthesis
- Beginning of statement
value1 = 1 + OuterFunction(nullptr,
InnerFunction(true,
false));
value2 = 2 +
OuterFunction(nullptr,
InnerFunction(true, false));
value3 = 3 +
InnerFunction(true,
false);- With parentheses, align new lines when i type them
- Align contents to opening parenthesis
- Indent new lines
- In existing code, use the setting for alignment of new lines within parenthesis
void Function(int a,
int b);- Indent case contents
- Indent case labels
- Indent braces following a case statement
switch (name) {
case 1:
{
break;
}
case 2:
break;
}- Indent braces of lambdas
auto square = [](int number) {
return number * number;
};
for_each(v.begin(), v.end(), [](int &number) {
number++;
});- Position of goto labels
- One indent to the left
- Move to the leftmost column
- Leave indented
class MyClass {
void Function() {
goto MyLabel;
MyLabel:
return;
}
}- Position of preprocessor directives
- One indent to the left
- Move to the leftmost column
- Leave indented
class MyClass {
void Function() {
#if DEBUG
DebugPrint();
#endif
}
}- Indent access specifier
class MyClass {
public:
MyClass();
}- Indent namespace contents
namespace MyNamespace
{
struct MyStruct {
int variable;
};
}- Preserve indentation of comments
#pragma once
//////////////////
// Declarations //
//////////////////
void Function1();
void Function2();- Position of Open Braces
- Position of open braces for namespaces
- Move to a new line
- Keep on the same line, but add a space before
- Don't automatically reposition
- Position of open braces for namespaces
namespace Namespace1 {
namespace Namespace2 {
}
}- Position of open braces for types
- Move to a new line
- Keep on the same line, but add a space before
- Don't automatically reposition
struct MyStruct {
int unused;
};
class Mylass1 {
int unused;
};- Position of open braces for functions
- Move to a new line
- Keep on the same line, but add a space before
- Don't automatically reposition
void Function2() {
}
void Class1::Function1() {
}- Position of open braces for control blocks
- Move to a new line
- Keep on the same line, but add a space before
- Don't automatically reposition
void Function() {
while (cin >> word) {
if (word.length() > 2) {
}
}
}- Position of open braces for lambdas
- Move to a new line
- Keep on the same line, but add a space before
- Don't automatically reposition
void Function() {
auto lambda1 = []() {
};
auto lambda2 = [&]() {
};
}- Position of open braces for scopes
- Place braces on separate lines
void Function() {
// Scope
{ int i;
}
}- Position of Close Braces
- For empty types, move closing braces to the same line as opening brace
- For empty function bodies, move closing braces to the same line as opening braces
class MyException {
}
MyClass::~MyClass() {
}- Position of Keywords
- Place 'catch' and similar keywords on a new line
- Place 'else' on a new line
- Place 'while' in a do-while loop on a new line
try {
} catch (...) {
}
if (a < b) {
} else {
}
do {
} while (false);-
Spacing for function parentheses
- Spacing between function names and opening parentheses of argument lists
- Insert a space
- Remove spaces
- Don't change spaces
- Insert space within parenthesis of an argument list
- Insert space between parenthesis when argument list is empty
- Spacing between function names and opening parentheses of argument lists
-
Spacing for control blocks
- Insert space between keyword and opening parenthesis
- Insert space within parenthesis of a control statement
-
Spacing for lambdas
- Insert space before opening parenthesis of lambda parameter list
-
Spacing for C-style cases
- Insert space within parenthesis of a C-style cast
- Insert space after closing parenthesis of C-style cast
-
Spacing for expressions
- Insert space within parenthesis of parenthesized expression
-
Spacing for braces
- Insert space before opening brace of blocks
- Insert space between empty braces
- Insert space before opening brace of uniform initialization and initializer list
- Insert space within braces of uniform initialization and initializer list
- Preserve spaces inside uniform initialization and initializer list
-
Spacing for brackets
- Insert space before opening square brackets
- Insert space within square brackets
- Insert space before empty square brackets
- Insert space between empty square brackets
- Group square brackets for multi-dimensional arrays together
- Insert space within square brackets for lambdas
- Insert space between empty square brackets for lambdas
-
Spacing for delimeters
- Insert space before commas
- Insert space after commas
- Remove spaces before and after member operators
- Insert spaces before colon for base in type declarations
- Insert spaces before colon for constructors
- Remove space before semicolons
- Insert space after semicolons
-
Spacing for operators
- Remove spaces between and after binary operators
- Binary operators
- Insert spaces before and after binary operators
- Remove spaces around binary operators
- Don't change spaces around binary operators
- Assignment operators
- Insert spaces around assignment operators
- Remove spaces around assignment operators
- Don't change spaces around assignment operators
- Pointer/reference alignment
- Align left
- Align center
- Align right
- Leave unchanged
- Conditional operator
- Insert spaces around conditional operators
- Remove spaces around conditional operators
- Don't change spaces around conditional operators
// Remove spacing between function names and opening parentheses of argument lists
// No space within parentheses of an argument lists
void Function1(int a) {
Function2(a);
}
// No space between parentheses when argument list is empty
void Function1()
{
Function2();
}
// Insert space between keyword and opening parenthesis
// No space within parenthesis of a control statement
void Function(int a, int b) {
if (a < b) {
}
}
// Insert space before opening parenthesis of lambda parameter list
void Function() {
auto square = [] (int a) { return a * a; };
}
// No space within parenthesis of a C-style cast
// No space after closing parenthesis of C-style cast
void Function(int a) {
double b = (double)a / 2;
}
// No space within parenthesis of parenthesized expression
void Function(int a) {
int b = (a + 1) / 2;
}
// Insert space before opening brace of blocks
if (i == 0) {
return 0;
}
// No space between empty braces
void Function() {
int *i = new int{};
if (i != nullptr) {}
}
// Insert space within braces of uniform initialization and initializer lists
void Function() {
vector<int> numbers = { 1, 2, 3 };
}
// Preserve spaces inside uniform initialization and initializer lists
void Function() {
vector<int> numbers = { 1, 2, 3,
10, 20, 30 };
}
// No space before/within square bracket
int Front(const vector<int> &numbers) {
return numbers[0];
}
// No space before empty square brackets
void Function() {
int numbers[] = { 1, 2, 3 };
}
// Group square brackets for multi-dimensional arrays together
void Function() {
int numbers [][3] = { 1, 2, 3 };
numbers [0][0] = 0;
}
// No space within square brackets for lambdas
void Function(int a) {
auto empty = [a] { return; };
}
// No space between empty square brackets for lambdas
void Function() {
auto empty = [] { return; };
}
// No spacing before commas
// Insert spacing after commas
void Function1(int a, int b) {
Function2(a, b);
}
// Remove spaces before and after member operators
void Function(vector<int> &numbers) {
numbers.push_back(0);
}
// No space before colon for base in type declarations
class MyClass: public MyBaseClass {
}
// No space before colon for constructors
MyClass::MyClass(): m_variable(0) {
}
// Remove space before semicolons
// Insert space after semicolons
void Function(int a) {
for (int i = 1; i <= a; i++)
cout << i << endl;
}
// Remove spaces between and after binary operators
void Function(int a, int b) {
a++;
++b;
}
// Spaces before and after binary operators
void Function(int a, int b) {
int sum = a + b;
}
// Pointer/reference alignment to the right
void Function(int *pInt, vector<int> &numbers);
// Insert spaces between condition operators
void Function(int a) {
bool positive = a > 0 ? true : false;
}