[PATCH] D54877: Update coding guidelines regarding use of `auto`

Stephen Kelly via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 25 06:37:56 PST 2018


steveire created this revision.
steveire added reviewers: aaron.ballman, chandlerc, dblaikie.
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D54877

Files:
  docs/CodingStandards.rst


Index: docs/CodingStandards.rst
===================================================================
--- docs/CodingStandards.rst
+++ docs/CodingStandards.rst
@@ -787,12 +787,17 @@
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 Some are advocating a policy of "almost always ``auto``" in C++11, however LLVM
-uses a more moderate stance. Use ``auto`` if and only if it makes the code more
-readable or easier to maintain. Don't "almost always" use ``auto``, but do use
-``auto`` with initializers like ``cast<Foo>(...)`` or other places where the
-type is already obvious from the context. Another time when ``auto`` works well
-for these purposes is when the type would have been abstracted away anyways,
-often behind a container's typedef such as ``std::vector<T>::iterator``.
+uses a more moderate stance. Don't "almost always" use ``auto``, but here are
+some cases where use of ``auto`` would make sense:
+
+* Where necessary, such as with lambda variables, inside templates etc.
+* With initializers like ``cast<Foo>(...)``.
+* Where the variable names makes it clear what it is for,
+  such as ``auto it`` instead of  ``std::vector<T>::iterator it``.
+* Range-based for loops.
+* Where the instance is only needed to perform a validity check,
+  such as a non-empty ``optional``, calling ``isValid()``, ``isNone()``,
+  comparing in an algorithm with an ``end()`` iterator etc.
 
 Beware unnecessary copies with ``auto``
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54877.175175.patch
Type: text/x-patch
Size: 1481 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181125/c208b271/attachment.bin>


More information about the llvm-commits mailing list