[Bug c++/110075] Bogus -Wdangling-reference

trashyankes at wp dot pl gcc-bugzilla@gcc.gnu.org
Mon Mar 30 18:53:29 GMT 2026


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

trashyankes at wp dot pl changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |trashyankes at wp dot pl

--- Comment #9 from trashyankes at wp dot pl ---
I do not think that adding attribute is "fix" for this.
In 15.2 from godbolt I have:

```
#include <string>

struct Dummy
{

};

const Dummy& getRefBy(const std::string& s);

int main()
{
    const auto& a = getRefBy("test"); //warning: possibly dangling reference to
a temporary [-Wdangling-reference]

    return 4;
}
```
https://gcc.godbolt.org/z/9c4c16dbe


How would even be possible to leak `s` there?
I would understand cases where we have `const char*` or `std::string*` there
but
object do not have any members.

Do GCC think that `std::string` have `Dummy` as payload? But then why
`std::wstring` have same problem but its illegal to try smuggle any objects
aside of characters itself in it?

Maybe `std::string` should be tagged with something to not trigger this waring
for unrelated types?


More information about the Gcc-bugs mailing list