Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3e4032a

Browse filesBrowse files
refackMylesBorins
authored andcommitted
doc: cleanup and references in C++ guide
PR-URL: #23650 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent ea9c44d commit 3e4032a
Copy full SHA for 3e4032a

File tree

Expand file treeCollapse file tree

1 file changed

+34
-23
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+34
-23
lines changed
Open diff view settings
Collapse file

‎CPP_STYLE_GUIDE.md‎

Copy file name to clipboardExpand all lines: CPP_STYLE_GUIDE.md
+34-23Lines changed: 34 additions & 23 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -192,40 +192,39 @@ class FancyContainer {
192192

193193
### Use `nullptr` instead of `NULL` or `0`
194194

195-
What it says in the title.
195+
Further reading in the [C++ Core Guidelines][ES.47].
196196

197197
### Ownership and Smart Pointers
198198

199-
"Smart" pointers are classes that act like pointers, e.g.
200-
by overloading the `*` and `->` operators. Some smart pointer types can be
201-
used to automate ownership bookkeeping, to ensure these responsibilities are
202-
met. `std::unique_ptr` is a smart pointer type introduced in C++11, which
203-
expresses exclusive ownership of a dynamically allocated object; the object
204-
is deleted when the `std::unique_ptr` goes out of scope. It cannot be
205-
copied, but can be moved to represent ownership transfer.
206-
`std::shared_ptr` is a smart pointer type that expresses shared ownership of a
207-
dynamically allocated object. `std::shared_ptr`s can be copied; ownership
208-
of the object is shared among all copies, and the object
209-
is deleted when the last `std::shared_ptr` is destroyed.
210-
211-
Prefer to use `std::unique_ptr` to make ownership
212-
transfer explicit. For example:
199+
* [R.20]: Use `std::unique_ptr` or `std::shared_ptr` to represent ownership
200+
* [R.21]: Prefer `unique_ptr` over `shared_ptr` unless you need to share
201+
ownership
202+
203+
Use `std::unique_ptr` to make ownership transfer explicit. For example:
213204

214205
```cpp
215206
std::unique_ptr<Foo> FooFactory();
216207
void FooConsumer(std::unique_ptr<Foo> ptr);
217208
```
218209
219-
Never use `std::auto_ptr`. Instead, use `std::unique_ptr`.
210+
Since `std::unique_ptr` has only move semantics, passing one by value transfers
211+
ownership to the callee and invalidates the caller's instance.
212+
213+
Don't use `std::auto_ptr`, it is deprecated ([Reference][cppref_auto_ptr]).
220214
221215
## Others
222216
223217
### Type casting
224218
225-
- Always avoid C-style casts (`(type)value`)
226-
- `dynamic_cast` does not work because RTTI is not enabled
227-
- Use `static_cast` for casting whenever it works
228-
- `reinterpret_cast` is okay if `static_cast` is not appropriate
219+
- Use `static_cast<T>` if casting is required, and it is valid
220+
- Use `reinterpret_cast` only when it is necessary
221+
- Avoid C-style casts (`(type)value`)
222+
- `dynamic_cast` does not work because Node.js is built without
223+
[Run Time Type Information][]
224+
225+
Further reading:
226+
* [ES.48]: Avoid casts
227+
* [ES.49]: If you must use a cast, use a named cast
229228
230229
### Using `auto`
231230
@@ -316,13 +315,25 @@ exports.foo = function(str) {
316315

317316
#### Avoid throwing JavaScript errors in nested C++ methods
318317

319-
When you have to throw the errors from C++, try to do it at the top level and
320-
not inside of nested calls.
318+
When you need to throw a JavaScript exception from C++ (i.e.
319+
`isolate()->ThrowException()`) prefer to do it as close to the return to JS as
320+
possible, and not inside of nested C++ calls. Since this changes the JS
321+
execution state doing it closest to where it is consumed reduces the chances of
322+
side effects.
321323

322-
Using C++ `throw` is not allowed.
324+
Node.js is built [without C++ exception handling][], so code using `throw` or
325+
even `try` and `catch` **will** break.
323326

324327

325328
[C++ Core Guidelines]: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
326329
[Google C++ Style Guide]: https://google.github.io/styleguide/cppguide.html
327330
[Google’s `cpplint`]: https://github.com/google/styleguide
328331
[errors]: https://github.com/nodejs/node/blob/master/doc/guides/using-internal-errors.md
332+
[ES.47]: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-nullptr
333+
[ES.48]: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts
334+
[ES.49]: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-casts-named
335+
[R.20]: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-owner
336+
[R.21]: http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rr-unique
337+
[Run Time Type Information]: https://en.wikipedia.org/wiki/Run-time_type_information
338+
[cppref_auto_ptr]: https://en.cppreference.com/w/cpp/memory/auto_ptr
339+
[without C++ exception handling]: https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_exceptions.html#intro.using.exception.no

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.