Skip to content

Commit eaff120

Browse files
addaleaxjasnell
authored andcommitted
doc: formalize non-const reference usage in C++ style guide
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>
1 parent 627f109 commit eaff120

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

CPP_STYLE_GUIDE.md

+38
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* [Memory allocation](#memory-allocation)
2020
* [Use `nullptr` instead of `NULL` or `0`](#use-nullptr-instead-of-null-or-0)
2121
* [Ownership and Smart Pointers](#ownership-and-smart-pointers)
22+
* [Avoid non-const references](#avoid-non-const-references)
2223
* [Others](#others)
2324
* [Type casting](#type-casting)
2425
* [Using `auto`](#using-auto)
@@ -212,6 +213,43 @@ ownership to the callee and invalidates the caller's instance.
212213
213214
Don't use `std::auto_ptr`, it is deprecated ([Reference][cppref_auto_ptr]).
214215
216+
### Avoid non-const references
217+
218+
Using non-const references often obscures which values are changed by an
219+
assignment. Consider using a pointer instead, which requires more explicit
220+
syntax to indicate that modifications take place.
221+
222+
```c++
223+
class ExampleClass {
224+
public:
225+
explicit ExampleClass(OtherClass* other_ptr) : pointer_to_other_(other_ptr) {}
226+
227+
void SomeMethod(const std::string& input_param,
228+
std::string* in_out_param); // Pointer instead of reference
229+
230+
const std::string& get_foo() const { return foo_string_; }
231+
void set_foo(const std::string& new_value) { foo_string_ = new_value; }
232+
233+
void ReplaceCharacterInFoo(char from, char to) {
234+
// A non-const reference is okay here, because the method name already tells
235+
// users that this modifies 'foo_string_' -- if that is not the case,
236+
// it can still be better to use an indexed for loop, or leave appropriate
237+
// comments.
238+
for (char& character : foo_string_) {
239+
if (character == from)
240+
character = to;
241+
}
242+
}
243+
244+
private:
245+
std::string foo_string_;
246+
// Pointer instead of reference. If this object 'owns' the other object,
247+
// this should be a `std::unique_ptr<OtherClass>`; a
248+
// `std::shared_ptr<OtherClass>` can also be a better choice.
249+
OtherClass* pointer_to_other_;
250+
};
251+
```
252+
215253
## Others
216254

217255
### Type casting

0 commit comments

Comments
 (0)