https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106238
--- Comment #6 from Romain Geissler <romain.geissler at amadeus dot com> --- Hi, After upgrading to the latest gcc trunk, I have hit another variant of this problem. For people just willing to exchange containers with swap, just swapping the arguments makes it work, and actually has the same logic: #include <set> void f(std::set<int>& a) { std::set<int> b; // b.swap(a); // Ok and has same effect a.swap(b); // KO } Compiled on x86_64 with -O2 -Werror=dangling-pointer: In file included from /opt/compiler-explorer/gcc-trunk-20230314/include/c++/13.0.1/set:62, from <source>:1: In member function 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::swap(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&) [with _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = std::less<int>; _Alloc = std::allocator<int>]', inlined from 'void std::set<_Key, _Compare, _Alloc>::swap(std::set<_Key, _Compare, _Alloc>&) [with _Key = int; _Compare = std::less<int>; _Alloc = std::allocator<int>]' at /opt/compiler-explorer/gcc-trunk-20230314/include/c++/13.0.1/bits/stl_set.h:443:18, inlined from 'void f(std::set<int>&)' at <source>:8:11: /opt/compiler-explorer/gcc-trunk-20230314/include/c++/13.0.1/bits/stl_tree.h:2092:36: error: storing the address of local variable 'b' in '*MEM[(struct _Rb_tree_node_base * &)&b + 16].std::_Rb_tree_node_base::_M_parent' [-Werror=dangling-pointer=] 2092 | __t._M_root()->_M_parent = __t._M_end(); | ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~ <source>: In function 'void f(std::set<int>&)': <source>:5:19: note: 'b' declared here 5 | std::set<int> b; | ^ <source>:5:19: note: 'b.std::set<int, std::less<int>, std::allocator<int> >::_M_t.std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::_M_impl.std::_Rb_tree<int, int, std::_Identity<int>, std::less<int>, std::allocator<int> >::_Rb_tree_impl<std::less<int>, true>::<unnamed>.std::_Rb_tree_header::_M_header.std::_Rb_tree_node_base::_M_parent' declared here cc1plus: some warnings being treated as errors Compiler returned: 1