[Bug c++/80542] Warn about accidental copying of data in range based for
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Apr 27 11:32:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80542
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |diagnostic
Status|UNCONFIRMED |NEW
Last reconfirmed| |2017-04-27
Ever confirmed|0 |1
Severity|normal |enhancement
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That's what 'auto' is for :-)
This could be helpful though. Code that intentionally wants to perform a
conversion to a different type doesn't need to use a const-reference, it could
just do:
for (std::pair<std::string, std::string> s : m)
That means there's a simple way (using fewer tokens!) to avoid the warning if a
conversion is really wanted.
It could be useful in other contexts too, this has the same problem:
const std::pair<std::string, std::string>& obj = *m.begin();
And again, if the conversion is intended there is no reason to use a reference
bound to a temporary instead of:
const std::pair<std::string, std::string> obj = *m.begin();
(People who prefer to former need to learn to trust copy elision).
In other contexts we probably wouldn't want to warn:
void foo(const std::pair<std::string, std::string>&);
foo(*m.begin());
This performs the same conversion but it's probably not possible to tell if a
warning is relevant.
More information about the Gcc-bugs
mailing list