https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117155
Bug ID: 117155 Summary: Bogus -Wdangling-reference warning after r13-3511-gd2249cd9adf Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: dimi...@unified-streaming.com Target Milestone: --- After r15-3941-g2531f014fb2364 ("c++: Implement -Wdangling-reference [PR106393]") for 106393, we are now seeing bogus instances of this new warning. Since the commit message marks this as an "experimental warning", I would suggest to not put this under -Wall, at least until it is no longer experimental, and false positives have been ferreted out. Reduced test case: $ cat dangling-ref.cpp #include <algorithm> #include <stdexcept> #include <string> #include <vector> template <typename Predicate> std::string const & find (std::vector<std::string> const &vec, Predicate predicate) { if (auto iter = std::find_if (vec.begin (), vec.end (), predicate); iter != vec.end ()) { return *iter; } throw std::runtime_error ("stream not found"); } struct match_t { match_t (std::string name) : name_ (std::move (name)) {} bool operator() (std::string const &s) const { return s == name_; } private: std::string name_; }; void f (std::string const &); void g (std::string const &name, std::vector<std::string> const &vec) { match_t match (name); auto const &s = find (vec, match); f (s); } $ ~/ins/gcc-13-3510-gf896c13489d/bin/g++ -Wall -c dangling-ref.cpp $ ~/ins/gcc-13-3511-gd2249cd9adf/bin/g++ -Wall -c dangling-ref.cpp dangling-ref.cpp: In function 'void g(const std::string&, const std::vector<std::__cxx11::basic_string<char> >&)': dangling-ref.cpp:39:15: warning: possibly dangling reference to a temporary [-Wdangling-reference] 39 | auto const &s = find (vec, match); | ^ dangling-ref.cpp:39:24: note: the temporary was destroyed at the end of the full expression 'find<match_t>((* & vec), match_t(match))' 39 | auto const &s = find (vec, match); | ~~~~~^~~~~~~~~~~~ Clearly, there is no dangling reference here, and not even a temporary. And even if there was a temporary, its lifetime should have been extended.