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 03803ee

Browse filesBrowse files
fhinkelMylesBorins
authored andcommitted
doc: mention smart pointers in Cpp style guide
Add rule for smart pointers, i.e., std::unique_ptr and std::shared_ptr, to the Cpp style guide. Mostly copied from the Google style guide. PR-URL: #17055 Ref: #16970 Ref: #16974 Ref: #17000 Ref: #17012 Ref: #17020 Ref: #17030 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent da8414e commit 03803ee
Copy full SHA for 03803ee

File tree

Expand file treeCollapse file tree

1 file changed

+25
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+25
-0
lines changed
Open diff view settings
Collapse file

‎CPP_STYLE_GUIDE.md‎

Copy file name to clipboardExpand all lines: CPP_STYLE_GUIDE.md
+25Lines changed: 25 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [`nullptr` instead of `NULL` or `0`](#nullptr-instead-of-null-or-0)
1717
* [Do not include `*.h` if `*-inl.h` has already been included](#do-not-include-h-if--inlh-has-already-been-included)
1818
* [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)
1920

2021
Unfortunately, the C++ linter (based on
2122
[Google’s `cpplint`](https://github.com/google/styleguide)), which can be run
@@ -168,3 +169,27 @@ A lot of code inside Node.js is written so that typechecking etc. is performed
168169
in JavaScript.
169170

170171
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.