-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
doc: formalize non-const reference usage in C++ style guide #23155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
73139a5
8daa18e
e9f8406
acba30d
04f941a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| * [Memory allocation](#memory-allocation) | ||
| * [Use `nullptr` instead of `NULL` or `0`](#use-nullptr-instead-of-null-or-0) | ||
| * [Ownership and Smart Pointers](#ownership-and-smart-pointers) | ||
| * [Avoid non-const references](#avoid-non-const-references) | ||
| * [Others](#others) | ||
| * [Type casting](#type-casting) | ||
| * [Do not include `*.h` if `*-inl.h` has already been included](#do-not-include-h-if--inlh-has-already-been-included) | ||
|
|
@@ -200,6 +201,42 @@ void FooConsumer(std::unique_ptr<Foo> ptr); | |
|
|
||
| Never use `std::auto_ptr`. Instead, use `std::unique_ptr`. | ||
|
|
||
| ### Avoid non-const references | ||
|
|
||
| Using non-const references often obscures which values are changed by an | ||
|
refack marked this conversation as resolved.
refack marked this conversation as resolved.
|
||
| assignment. A pointer is almost always a better choice. | ||
|
|
||
| ```c++ | ||
| class ExampleClass { | ||
| public: | ||
| explicit ExampleClass(OtherClass* other_ptr) : pointer_to_other_(other_ptr) {} | ||
|
|
||
| void SomeMethod(const std::string& input_param, | ||
| std::string* in_out_param); // Pointer instead of reference | ||
|
|
||
| const std::string& get_foo() const { return foo_string_; } | ||
| void set_foo(const std::string& new_value) { foo_string_ = new_value; } | ||
|
|
||
| void ReplaceCharacterInFoo(char from, char to) { | ||
| // A non-const reference is okay here, because the method name already tells | ||
| // users that this modifies 'foo_string_' -- if that is not the case, | ||
| // it can still be better to use an indexed for loop, or leave appropriate | ||
| // comments. | ||
| for (char& character : foo_string_) { | ||
| if (character == from) | ||
| character = to; | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| std::string foo_string_; | ||
| // Pointer instead of reference. If this objects 'owns' the other object, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: object
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thefourtheye Thanks for catching, done! |
||
| // this should be be a `std::unique_ptr<OtherClass>`; a | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: double |
||
| // `std::shared_ptr<OtherClass>` can also be a better choice. | ||
| OtherClass* pointer_to_other_; | ||
| }; | ||
| ``` | ||
|
|
||
| ## Others | ||
|
|
||
| ### Type casting | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.