https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95911
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution|--- |DUPLICATE
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to 。 from comment #0)
> In my
> comprehension of c++, the second class A veriable should not be deleted.
Your comprehension is wrong.
The temporary is bound to the parameter of bug(A&&) but that does not extend
its lifetime, so the temporary is destroyed at the end of the full expression.
That leaves 'b' as a dangling reference.
In general it's not possible to warn about this code because the compiler
doesn't know that bug(newA()) returns a reference to a temporary (if the
function isn't inlined, it's impossible to know that).
> When double using function bug(see the .ii file), you can find it was not
> deleted so early.
No, it's deleted at exactly the same time (the end of the full expression).
Code like this is buggy and dangerous. We already have Bug 49974 requesting a
warning for it.
*** This bug has been marked as a duplicate of bug 49974 ***