Skip to content

Commit d3e76e7

Browse files
fhinkelMylesBorins
authored andcommitted
doc: introduce categories to Cpp style guide
Ref: #17052 (comment) PR-URL: #17095 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 90a5e9f commit d3e76e7

File tree

1 file changed

+55
-47
lines changed

1 file changed

+55
-47
lines changed

CPP_STYLE_GUIDE.md

+55-47
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@
22

33
## Table of Contents
44

5-
* [Left-leaning (C++ style) asterisks for pointer declarations](#left-leaning-c-style-asterisks-for-pointer-declarations)
6-
* [2 spaces of indentation for blocks or bodies of conditionals](#2-spaces-of-indentation-for-blocks-or-bodies-of-conditionals)
7-
* [4 spaces of indentation for statement continuations](#4-spaces-of-indentation-for-statement-continuations)
8-
* [Align function arguments vertically](#align-function-arguments-vertically)
9-
* [Initialization lists](#initialization-lists)
10-
* [CamelCase for methods, functions, and classes](#camelcase-for-methods-functions-and-classes)
11-
* [snake\_case for local variables and parameters](#snake_case-for-local-variables-and-parameters)
12-
* [snake\_case\_ for private class fields](#snake_case_-for-private-class-fields)
13-
* [Space after `template`](#space-after-template)
14-
* [Type casting](#type-casting)
15-
* [Memory allocation](#memory-allocation)
16-
* [`nullptr` instead of `NULL` or `0`](#nullptr-instead-of-null-or-0)
17-
* [Do not include `*.h` if `*-inl.h` has already been included](#do-not-include-h-if--inlh-has-already-been-included)
18-
* [Avoid throwing JavaScript errors in nested C++ methods](#avoid-throwing-javascript-errors-in-nested-c-methods)
19-
* [Ownership and Smart Pointers](#ownership-and-smart-pointers)
5+
* [Formatting](#formatting)
6+
* [Left-leaning (C++ style) asterisks for pointer declarations](#left-leaning-c-style-asterisks-for-pointer-declarations)
7+
* [2 spaces of indentation for blocks or bodies of conditionals](#2-spaces-of-indentation-for-blocks-or-bodies-of-conditionals)
8+
* [4 spaces of indentation for statement continuations](#4-spaces-of-indentation-for-statement-continuations)
9+
* [Align function arguments vertically](#align-function-arguments-vertically)
10+
* [Initialization lists](#initialization-lists)
11+
* [CamelCase for methods, functions and classes](#camelcase-for-methods-functions-and-classes)
12+
* [snake\_case for local variables and parameters](#snake_case-for-local-variables-and-parameters)
13+
* [snake\_case\_ for private class fields](#snake_case_-for-private-class-fields)
14+
* [Space after `template`](#space-after-template)
15+
* [Memory Management](#memory-management)
16+
* [Memory allocation](#memory-allocation)
17+
* [Use `nullptr` instead of `NULL` or `0`](#use-nullptr-instead-of-null-or-0)
18+
* [Ownership and Smart Pointers](#ownership-and-smart-pointers)
19+
* [Others](#others)
20+
* [Type casting](#type-casting)
21+
* [Do not include `*.h` if `*-inl.h` has already been included](#do-not-include-h-if--inlh-has-already-been-included)
22+
* [Avoid throwing JavaScript errors in nested C++ methods](#avoid-throwing-javascript-errors-in-nested-c-methods)
2023

2124
Unfortunately, the C++ linter (based on
2225
[Google’s `cpplint`](https://github.com/google/styleguide)), which can be run
2326
explicitly via `make lint-cpp`, does not currently catch a lot of rules that are
2427
specific to the Node.js C++ code base. This document explains the most common of
2528
these rules:
2629

30+
## Formatting
31+
2732
## Left-leaning (C++ style) asterisks for pointer declarations
2833

2934
`char* buffer;` instead of `char *buffer;`
@@ -128,23 +133,50 @@ class FancyContainer {
128133
...
129134
}
130135
```
131-
132-
## Type casting
133-
134-
- Always avoid C-style casts (`(type)value`)
135-
- `dynamic_cast` does not work because RTTI is not enabled
136-
- Use `static_cast` for casting whenever it works
137-
- `reinterpret_cast` is okay if `static_cast` is not appropriate
136+
## Memory Management
138137
139138
## Memory allocation
140139
141140
- `Malloc()`, `Calloc()`, etc. from `util.h` abort in Out-of-Memory situations
142141
- `UncheckedMalloc()`, etc. return `nullptr` in OOM situations
143142
144-
## `nullptr` instead of `NULL` or `0`
143+
## Use `nullptr` instead of `NULL` or `0`
145144
146145
What it says in the title.
147146
147+
## Ownership and Smart Pointers
148+
149+
"Smart" pointers are classes that act like pointers, e.g.
150+
by overloading the `*` and `->` operators. Some smart pointer types can be
151+
used to automate ownership bookkeeping, to ensure these responsibilities are
152+
met. `std::unique_ptr` is a smart pointer type introduced in C++11, which
153+
expresses exclusive ownership of a dynamically allocated object; the object
154+
is deleted when the `std::unique_ptr` goes out of scope. It cannot be
155+
copied, but can be moved to represent ownership transfer.
156+
`std::shared_ptr` is a smart pointer type that expresses shared ownership of a
157+
dynamically allocated object. `std::shared_ptr`s can be copied; ownership
158+
of the object is shared among all copies, and the object
159+
is deleted when the last `std::shared_ptr` is destroyed.
160+
161+
Prefer to use `std::unique_ptr` to make ownership
162+
transfer explicit. For example:
163+
164+
```cpp
165+
std::unique_ptr<Foo> FooFactory();
166+
void FooConsumer(std::unique_ptr<Foo> ptr);
167+
```
168+
169+
Never use `std::auto_ptr`. Instead, use `std::unique_ptr`.
170+
171+
## Others
172+
173+
## Type casting
174+
175+
- Always avoid C-style casts (`(type)value`)
176+
- `dynamic_cast` does not work because RTTI is not enabled
177+
- Use `static_cast` for casting whenever it works
178+
- `reinterpret_cast` is okay if `static_cast` is not appropriate
179+
148180
## Do not include `*.h` if `*-inl.h` has already been included
149181

150182
Do
@@ -169,27 +201,3 @@ A lot of code inside Node.js is written so that typechecking etc. is performed
169201
in JavaScript.
170202

171203
Using C++ `throw` is not allowed.
172-
173-
## Ownership and Smart Pointers
174-
175-
"Smart" pointers are classes that act like pointers, e.g.
176-
by overloading the `*` and `->` operators. Some smart pointer types can be
177-
used to automate ownership bookkeeping, to ensure these responsibilities are
178-
met. `std::unique_ptr` is a smart pointer type introduced in C++11, which
179-
expresses exclusive ownership of a dynamically allocated object; the object
180-
is deleted when the `std::unique_ptr` goes out of scope. It cannot be
181-
copied, but can be moved to represent ownership transfer.
182-
`std::shared_ptr` is a smart pointer type that expresses shared ownership of a
183-
dynamically allocated object. `std::shared_ptr`s can be copied; ownership
184-
of the object is shared among all copies, and the object
185-
is deleted when the last `std::shared_ptr` is destroyed.
186-
187-
Prefer to use `std::unique_ptr` to make ownership
188-
transfer explicit. For example:
189-
190-
```cpp
191-
std::unique_ptr<Foo> FooFactory();
192-
void FooConsumer(std::unique_ptr<Foo> ptr);
193-
```
194-
195-
Never use `std::auto_ptr`. Instead, use `std::unique_ptr`.

0 commit comments

Comments
 (0)