cppunit-1.15.1 and a few other projects fail to compile due to -Werror on -Werror=dangling-reference. I think those are false positives. Extracted example from cppunit: // $ cat a.cpp.cpp #include <vector> int attributesAsString(std::vector<int> & v) { int attributes; std::vector<int>::const_iterator itAttribute = v.begin(); while ( itAttribute != v.end() ) { const int &attribute = *itAttribute++; attributes += attribute; } return attributes; } $ g++-13.0.0 -Werror=dangling-reference -c a.cpp.cpp -o a.o a.cpp.cpp: In function 'int attributesAsString(std::vector<int>&)': a.cpp.cpp:12:16: error: possibly dangling reference to a temporary [-Werror=dangling-reference] 12 | const int &attribute = *itAttribute++; | ^~~~~~~~~ a.cpp.cpp:12:40: note: the temporary was destroyed at the end of the full expression 'itAttribute.__gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator++(0).__gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator*()' 12 | const int &attribute = *itAttribute++; | ^~ $ g++-13.0.0 -v Using built-in specs. COLLECT_GCC=/<<NIX>>/gcc-13.0.0/bin/g++ COLLECT_LTO_WRAPPER=/<<NIX>>/gcc-13.0.0/libexec/gcc/x86_64-unknown-linux-gnu/13.0.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: Thread model: posix Supported LTO compression algorithms: zlib gcc version 13.0.0 20221030 (experimental) (GCC)
I figure this warning is a false positive since "const int &attribute" refers to one of the int elements of the vector v, not to a temporary object. I think the warning should probably be disabled for operator*, since that returns a reference but doesn't return its parameter: reference operator*() const _GLIBCXX_NOEXCEPT { return *_M_current; } Patch: --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -13467,7 +13467,11 @@ do_warn_dangling_reference (tree expr) can be e.g. const int& z = std::min({1, 2, 3, 4, 5, 6, 7}); which doesn't dangle: std::min here returns an int. */ - || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl)))) + || !TYPE_REF_OBJ_P (TREE_TYPE (TREE_TYPE (fndecl))) + /* XXX */ + || (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) + && DECL_OVERLOADED_OPERATOR_P (fndecl) + && DECL_OVERLOADED_OPERATOR_IS (fndecl, INDIRECT_REF))) return NULL_TREE; /* Here we're looking to see if any of the arguments is a temporary
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>: https://gcc.gnu.org/g:32a06ce38a38bf37db468f0e6c83520fcc221534 commit r13-3642-g32a06ce38a38bf37db468f0e6c83520fcc221534 Author: Marek Polacek <polacek@redhat.com> Date: Tue Nov 1 17:05:52 2022 -0400 c++: Quash -Wdangling-reference for member operator* [PR107488] -Wdangling-reference complains here: std::vector<int> v = ...; std::vector<int>::const_iterator it = v.begin(); while (it != v.end()) { const int &r = *it++; // warning } because it sees a call to __gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator* which returns a reference and its argument is a TARGET_EXPR representing the result of __gnu_cxx::__normal_iterator<const int*, std::vector<int> >::operator++ But 'r' above refers to one of the int elements of the vector 'v', not to a temporary object. Therefore the warning is a false positive. I suppose code like the above is relatively common (the warning broke cppunit-1.15.1 and a few other projects), so presumably it makes sense to suppress the warning when it comes to member operator*. In this case it's defined as reference operator*() const _GLIBCXX_NOEXCEPT { return *_M_current; } and I'm guessing a lot of member operator* are like that, at least when it comes to iterators. I've looked at _Fwd_list_iterator, _Fwd_list_const_iterator, __shared_ptr_access, _Deque_iterator, istream_iterator, etc, and they're all like that, so adding #pragmas would be quite tedious. :/ PR c++/107488 gcc/cp/ChangeLog: * call.cc (do_warn_dangling_reference): Quash -Wdangling-reference for member operator*. gcc/testsuite/ChangeLog: * g++.dg/warn/Wdangling-reference5.C: New test.
Should be fixed, thanks for the report.
Thank you! cppunit-1.15.1 now builds fine with your fix.