-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Style notes on passing and storing object addresses #4310
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
docs/project/cpp_style_guide.md
Outdated
- When storing unowned objects as members, prefer to pass by reference and | ||
store as a pointer. For example, `Foo* foo_;` and | ||
`Bar(Foo& foo) : foo_(&foo)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on further discussion, let's keep the storing part but add some more explicit guidance for passing. This is allowed and slightly encouraged in the Google style guide, but I think we can be a bit more direct and explicit for Carbon:
- When storing unowned objects as members, prefer to pass by reference and | |
store as a pointer. For example, `Foo* foo_;` and | |
`Bar(Foo& foo) : foo_(&foo)`. | |
- When passing a reference to an unowned object as an argument, use a reference | |
unless one of the following cases applies: | |
- If it 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 an document that it must not be null (unless it is also optional). | |
- When storing a reference to an unowned object as a member, prefer to store as a | |
pointer. For example, `Foo* foo_;` and `Bar(Foo* foo) : foo_(foo)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulled, and made some edits (including "when passing/storing a reference to an unowned object" -> "when passing/storing an object's address" in order to avoid different meanings of "reference", cleaning up the example)
I think the thread is coming to consensus now, and suggested an update to match what I think I'm seeing there... but also maybe I'm misreading so good to use the suggested change to make sure we're all on the same page. |
Co-authored-by: Chandler Carruth <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, nice rewording too. =]
Context. Some relevant Google C++ style is at Inputs and Outputs. #4301 is an example application of the style.