[Bug c++/80542] New: Warn about accidental copying of data in range based for

antoshkka at gmail dot com gcc-bugzilla@gcc.gnu.org
Thu Apr 27 11:05:00 GMT 2017


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80542

            Bug ID: 80542
           Summary: Warn about accidental copying of data in range based
                    for
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Here's a small example with accidental copying that could be often seen in
code:

```
#include <map>
#include <string>
#include <iostream>

int main() {
    std::map<std::string, std::string> m = {
        {"This is ", "an exmple, "},
        {"were user ", "accidently copies"},
    };


    for (const std::pair<std::string, std::string>& s : m) {
        std::cout << s.first << s.second;
    }
}
```

User provided `const std::pair<std::string, std::string>&` type instead of
`const std::pair<const std::string, std::string>&` so the std::pair is copied.


Please add a "Value will be copied because types differ" warning for range
based for loops and show it when:
user provides a const reference type in for-range-declaration and that type
when decayed differs from decayed iterator dereference type.


More information about the Gcc-bugs mailing list