[Bug c++/49974] missing warning for indirectly returning reference to local/temporary

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Aug 4 12:40:00 GMT 2011


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49974

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2011-08-04 12:40:15 UTC ---
Maybe related, but not the same.  PR986 involves creating a temporary and
binding the reference to it, that should be easier to warn about.  Here's a
similar case to PR986 where a reference member is dangling after the
constructor, which we fail to warn about:

struct Y {
    Y(int local) : ref(local) { }
    int& ref;
};

That, and the example in 986, should be easy to warn about.  When the reference
is bound we can know if the initializer is local/temporary.

This PR is different, the references passed to f and Y::Y are valid during
those calls. The reference only becomes invalid when it escapes from the
function that declares the local variable.  This requires some more complex
analysis, only possible if the bodies of f and Y::Y are visible, and maybe only
possible with optimization enabled so inlining happens.



More information about the Gcc-bugs mailing list