llvm.org GIT mirror llvm / feed880
C++11: Remove const from in auto guidelines Using const is orthogonal to guidelines on using auto& and auto*. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203257 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan P. N. Exon Smith 5 years ago
1 changed file(s) with 8 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
745745 is a copy. Particularly in range-based ``for`` loops, careless copies are
746746 expensive.
747747
748 As a rule of thumb, use ``const auto &`` unless you need to mutate or copy the
749 result, and use ``const auto *`` when copying pointers.
750
751 .. code-block:: c++
752
753 // Typically there's no reason to mutate or modify Val.
748 As a rule of thumb, use ``auto &`` unless you need to copy the result, and use
749 ``auto *`` when copying pointers.
750
751 .. code-block:: c++
752
753 // Typically there's no reason to copy.
754754 for (const auto &Val : Container) { observe(Val); }
755
756 // Remove the const if you need to modify Val.
757755 for (auto &Val : Container) { Val.change(); }
758756
759757 // Remove the reference if you really want a new copy.
760758 for (auto Val : Container) { Val.change(); saveSomewhere(Val); }
761759
762760 // Copy pointers, but make it clear that they're pointers.
763 for (const auto *Val : Container) { observe(*Val); }
764 for (auto *Val : Container) { Val->change(); }
761 for (const auto *Ptr : Container) { observe(*Ptr); }
762 for (auto *Ptr : Container) { Ptr->change(); }
765763
766764 Style Issues
767765 ============