Skip to content

Commit

Permalink
Style notes on passing and storing object addresses (#4310)
Browse files Browse the repository at this point in the history
[Context](https://discord.com/channels/655572317891461132/821113559755784242/1283516297686286377).
Some relevant Google C++ style is at [Inputs and
Outputs](https://google.github.io/styleguide/cppguide.html#Inputs_and_Outputs).
#4301 is an example application of the style.

---------

Co-authored-by: Chandler Carruth <[email protected]>
  • Loading branch information
jonmeow and chandlerc authored Sep 20, 2024
1 parent 9b0519d commit 434ee32
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/project/cpp_style_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ these.
although these guidelines differ slightly.
- Always mark constructors `explicit` unless there's a specific reason to
support implicit or `{}` initialization.
- When passing an object's address as an argument, use a reference unless one
of the following cases applies:

- If the parameter is optional, use a pointer and document that it may be
null.
- If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).

- When storing an object's address as a non-owned member, prefer
storing a pointer. For example:

```cpp
class Bar {
public:
// `foo` must not be null.
explicit Bar(Foo* foo) : foo_(foo) {}
private:
Foo* foo_;
};
```

- Always use braces for conditional, `switch`, and loop statements, even when
the body is a single statement.
- Within a `switch` statement, use braces after a `case` label when
Expand Down

0 comments on commit 434ee32

Please sign in to comment.