@@ -192,40 +192,39 @@ class FancyContainer {
192
192
193
193
### Use ` nullptr ` instead of ` NULL ` or ` 0 `
194
194
195
- What it says in the title .
195
+ Further reading in the [ C++ Core Guidelines ] [ ES.47 ] .
196
196
197
197
### Ownership and Smart Pointers
198
198
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:
213
204
214
205
``` cpp
215
206
std::unique_ptr<Foo> FooFactory ();
216
207
void FooConsumer (std::unique_ptr<Foo > ptr);
217
208
```
218
209
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]).
220
214
221
215
## Others
222
216
223
217
### Type casting
224
218
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
229
228
230
229
### Using `auto`
231
230
@@ -316,13 +315,25 @@ exports.foo = function(str) {
316
315
317
316
#### Avoid throwing JavaScript errors in nested C++ methods
318
317
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.
321
323
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.
323
326
324
327
325
328
[ C++ Core Guidelines ] : http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines
326
329
[ Google C++ Style Guide ] : https://google.github.io/styleguide/cppguide.html
327
330
[ Google’s `cpplint` ] : https://github.com/google/styleguide
328
331
[ 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