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(...)`` 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::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(...)``. +* Where the variable names makes it clear what it is for, + such as ``auto it`` instead of ``std::vector::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`` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^