https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115229
Bug ID: 115229 Summary: inconsistent `error: possibly dangling reference to a temporary` Product: gcc Version: 15.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: pobrn at protonmail dot com Target Milestone: --- Consider the following piece of C++ code: ``` struct A { int q; int *p; }; struct B { A value_or() { return A {}; } }; B f(); void test() { const auto& x = f().value_or(); } ``` gcc 15, 14 show the following diagnostic: ``` <source>: In function 'void test()': <source>:15:21: error: possibly dangling reference to a temporary [-Werror=dangling-reference] 15 | const auto& x = f().value_or(); | ^ <source>:15:37: note: the temporary was destroyed at the end of the full expression 'f().B::value_or()' 15 | const auto& x = f().value_or(); | ~~~~~~~~~~~~^~ ``` while gcc 13 does not. Could someone help me understand why this warning is even shown? I would assume lifetime extension should extend the lifetime of the returned value as needed. Note that removing `int *p` from `A` makes the warning go away, so I am not sure if this warning has anything to do with the lifetime of the returned value, but then I am confused why the error points to `x`.