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

2021-05-10 Thread redi at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80542

Jonathan Wakely  changed:

   What|Removed |Added

 Status|NEW |RESOLVED
 Resolution|--- |DUPLICATE

--- Comment #3 from Jonathan Wakely  ---
Yes, thanks. Let's close it as a dup of the bug that added the warning (even
though this one is older).

*** This bug has been marked as a duplicate of bug 94695 ***

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

2021-05-09 Thread antoshkka at gmail dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80542

--- Comment #2 from Antony Polukhin  ---
This issue could be closed. GCC 11 has the required -Wrange-loop-construct
warning: https://godbolt.org/z/343M6WMjb

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

2017-04-27 Thread redi at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80542

Jonathan Wakely  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  ---
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 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& 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 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&);
  foo(*m.begin());

This performs the same conversion but it's probably not possible to tell if a
warning is relevant.