Skip to content
Browse files

doc: formalize `auto` usage in C++ style guide

We generally avoid using `auto` if not necessary. This
formalizes this rules by writing them down in the C++ style guide.

PR-URL: #23028
Reviewed-By: Tobias Nießen <>
Reviewed-By: Luigi Pinca <>
Reviewed-By: Refael Ackermann <>
Reviewed-By: Joyee Cheung <>
Reviewed-By: Gireesh Punathil <>
Reviewed-By: Daniel Bevenius <>
Reviewed-By: Sakthipriyan Vairamani <>
Reviewed-By: Michael Dawson <>
  • Loading branch information
addaleax committed Sep 30, 2018
1 parent 847037e commit aba7677e826065fc1f1025ee2db5faee035fd515
Showing with 19 additions and 0 deletions.
  1. +19 −0
@@ -20,6 +20,7 @@
* [Ownership and Smart Pointers](#ownership-and-smart-pointers)
* [Others](#others)
* [Type casting](#type-casting)
* [Using `auto`](#using-auto)
* [Do not include `*.h` if `*-inl.h` has already been included](#do-not-include-h-if--inlh-has-already-been-included)
* [Avoid throwing JavaScript errors in C++ methods](#avoid-throwing-javascript-errors-in-c)
* [Avoid throwing JavaScript errors in nested C++ methods](#avoid-throwing-javascript-errors-in-nested-c-methods)
@@ -209,6 +210,24 @@ Never use `std::auto_ptr`. Instead, use `std::unique_ptr`.
- Use `static_cast` for casting whenever it works
- `reinterpret_cast` is okay if `static_cast` is not appropriate
### Using `auto`
Being explicit about types is usually preferred over using `auto`.
Use `auto` to avoid type names that are noisy, obvious, or unimportant. When
doing so, keep in mind that explicit types often help with readability and
verifying the correctness of code.
for (const auto& item : some_map) {
const KeyType& key = item.first;
const ValType& value = item.second;
// The rest of the loop can now just refer to key and value,
// a reader can see the types in question, and we've avoided
// the too-common case of extra copies in this iteration.
### Do not include `*.h` if `*-inl.h` has already been included

0 comments on commit aba7677

Please sign in to comment.
You can’t perform that action at this time.