فهرست منبع

Style notes on passing and storing object addresses (#4310)

[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 <chandlerc@gmail.com>
Jon Ross-Perkins 1 سال پیش
والد
کامیت
434ee32515
1فایلهای تغییر یافته به همراه22 افزوده شده و 0 حذف شده
  1. 22 0
      docs/project/cpp_style_guide.md

+ 22 - 0
docs/project/cpp_style_guide.md

@@ -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