C++11: Remove const from in auto guidelines

Using const is orthogonal to guidelines on using auto& and auto*.

llvm-svn: 203257
This commit is contained in:
Duncan P. N. Exon Smith 2014-03-07 18:06:15 +00:00
parent d723f5186e
commit fdbb44a59e
1 changed files with 5 additions and 7 deletions

View File

@ -746,23 +746,21 @@ The convenience of ``auto`` makes it easy to forget that its default behavior
is a copy. Particularly in range-based ``for`` loops, careless copies are is a copy. Particularly in range-based ``for`` loops, careless copies are
expensive. expensive.
As a rule of thumb, use ``const auto &`` unless you need to mutate or copy the As a rule of thumb, use ``auto &`` unless you need to copy the result, and use
result, and use ``const auto *`` when copying pointers. ``auto *`` when copying pointers.
.. code-block:: c++ .. code-block:: c++
// Typically there's no reason to mutate or modify Val. // Typically there's no reason to copy.
for (const auto &Val : Container) { observe(Val); } for (const auto &Val : Container) { observe(Val); }
// Remove the const if you need to modify Val.
for (auto &Val : Container) { Val.change(); } for (auto &Val : Container) { Val.change(); }
// Remove the reference if you really want a new copy. // Remove the reference if you really want a new copy.
for (auto Val : Container) { Val.change(); saveSomewhere(Val); } for (auto Val : Container) { Val.change(); saveSomewhere(Val); }
// Copy pointers, but make it clear that they're pointers. // Copy pointers, but make it clear that they're pointers.
for (const auto *Val : Container) { observe(*Val); } for (const auto *Ptr : Container) { observe(*Ptr); }
for (auto *Val : Container) { Val->change(); } for (auto *Ptr : Container) { Ptr->change(); }
Style Issues Style Issues
============ ============