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

Roy Stogner <roystgnr at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |roystgnr at gmail dot com

--- Comment #2 from Roy Stogner <roystgnr at gmail dot com> ---
This case looks most similar to the one that my code triggered, which I
simplified to the following:

---

#include <map>

const double &
find_wrapper(const std::map<int, double> & map,
             const int & key)
{
  auto it = map.find(key);
  return it->second;
}


int main(void)
{
  std::map<int, double> testmap{{1,0.0}};
  const double & d = find_wrapper(testmap, 1);
  return int(d);
}

---

$ g++ -Wall -Wextra -Werror -o test.x test.C
test.C: In function ‘int main()’:
test.C:15:18: error: possibly dangling reference to a temporary
[-Werror=dangling-reference]
   15 |   const double & d = find_wrapper(testmap, 1);
      |                  ^
test.C:15:34: note: the temporary was destroyed at the end of the full
expression ‘find_wrapper(testmap, 1)’
   15 |   const double & d = find_wrapper(testmap, 1);
      |                      ~~~~~~~~~~~~^~~~~~~~~~~~
cc1plus: all warnings being treated as errors

---

If I change the parameter type to `const int key`, then the warning is not
triggered.  If I leave the parameter type as a reference but I pass in a
non-temporary variable "one" instead of "1", then the warning is not triggered.


Either or both of these cases might be a dup of bug 109642

Reply via email to