From 694ed1793c463a6d288dd8e2e833262acde7fff1 Mon Sep 17 00:00:00 2001 From: Karl Wiberg Date: Tue, 6 Nov 2018 17:47:26 +0100 Subject: [PATCH] Add a style rule about not using const optional& arguments Motivated by discussions here: https://webrtc-review.googlesource.com/c/src/+/109583 Bug: none Change-Id: Ia0723adf9fa7c970137ffc9cb5612cb3360d7f5f Notry: true Reviewed-on: https://webrtc-review.googlesource.com/c/109568 Reviewed-by: Danil Chapovalov Reviewed-by: Niels Moller Commit-Queue: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#25556} --- style-guide.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/style-guide.md b/style-guide.md index 2a35fdc5d1..391c45b644 100644 --- a/style-guide.md +++ b/style-guide.md @@ -77,6 +77,18 @@ instead of | use See [the source](api/array_view.h) for more detailed docs. +### `absl::optional` as function argument + +`absl::optional` is generally a good choice when you want to pass a +possibly missing `T` to a function—provided of course that `T` +is a type that it makes sense to pass by value. + +However, when you want to avoid pass-by-value, generally **do not pass +`const absl::optional&`; use `const T*` instead.** `const +absl::optional&` forces the caller to store the `T` in an +`absl::optional`; `const T*`, on the other hand, makes no +assumptions about how the `T` is stored. + ### sigslot sigslot is a lightweight library that adds a signal/slot language