Skip to content

Commit

Permalink
doc: formalize non-const reference usage in C++ style guide
Browse files Browse the repository at this point in the history
We generally avoid using non-const references if not necessary. This
formalizes this rules by writing them down in the C++ style guide.

(Note: Some reviews are from the original PR.)

Refs: #23028
PR-URL: #23155
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
  • Loading branch information
addaleax committed Oct 21, 2018
1 parent 87b808f commit 5550510
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions CPP_STYLE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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)
* [Using `auto`](#using-auto)
Expand Down Expand Up @@ -212,6 +213,43 @@ ownership to the callee and invalidates the caller's instance.
Don't use `std::auto_ptr`, it is deprecated ([Reference][cppref_auto_ptr]).
### Avoid non-const references
Using non-const references often obscures which values are changed by an
assignment. Consider using a pointer instead, which requires more explicit
syntax to indicate that modifications take place.
```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 object 'owns' the other object,
// this should be a `std::unique_ptr<OtherClass>`; a
// `std::shared_ptr<OtherClass>` can also be a better choice.
OtherClass* pointer_to_other_;
};
```

## Others

### Type casting
Expand Down

0 comments on commit 5550510

Please sign in to comment.