[Bug c++/102530] Warn about non-extended temporary passed to a function returning a reference in temp-extending context

jason at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Wed Sep 29 16:20:15 GMT 2021


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

--- Comment #3 from Jason Merrill <jason at gcc dot gnu.org> ---
A simpler alternative would be to warn about any non-extended temporaries in an
initializer.  This would have both more false positives and fewer false
negatives.

If the temporary is passed by value or rvalue reference to another function,
it's probably OK.  If it's passed by lvalue reference, including as 'this',
it's dangerous.

The reference-like class issue also affects what we are initializing;
initializing an iterator from container().begin() has the same problem.  As can
a pointer:

const char *p = string().c_str();

Getting something more complex than the "simpler alternative" to a low enough
level of false negatives would probably require analysis of types and functions
to recognize reference-like classes and functions that return internal
references/pointers.


More information about the Gcc-bugs mailing list