This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/80542] Warn about accidental copying of data in range based for
- From: "redi at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 27 Apr 2017 11:32:41 +0000
- Subject: [Bug c++/80542] Warn about accidental copying of data in range based for
- Auto-submitted: auto-generated
- References: <bug-80542-4@http.gcc.gnu.org/bugzilla/>
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.