range for
summary
Range-based for often copies all the objects by using plain auto; prefer const auto& or a reference when mutation isn’t intended. Using auto&& creates a forwarding reference that can bind to lvalues (becoming an lvalue reference) or rvalues, so blindly moving from it can destroy live objects (int is harmless but string would be damaged). C++17 changes the expansion to add an extra auto so begin and end can be different types, enabling iterator/sentinel ranges.
AI-written summary · about








