This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: g++ 2.5.2 does not catch reference to local variable error.
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: LIM Fung-Chai <lim dot fung dot chai at gmail dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 4 Aug 2011 10:20:20 +0100
- Subject: Re: g++ 2.5.2 does not catch reference to local variable error.
- References: <CAPTxc7jzasuny64pbtHSjK+QP8fduvhR94c9W6X5H==0UfgJ7A@mail.gmail.com>
On 4 August 2011 03:30, LIM Fung-Chai <lim.fung.chai@gmail.com> wrote:
> Hi,
>
> "g++ -Wall -Wextra ..." should flag a warning on the following code
> but does not.
>
> std::pair<int, const XYZ&>
> get_XYZ_data()
> {
> ? ?XYZ result;
> ? ?return std::pair<int, const XYZ&>(1, result);
> }
>
> This is a violation of Scott Meyer's "Effective C++" Item 21 "Don't
> try to return a reference when you must return an object." ?GCC
> version 4.5.2 on Kubuntu 11.04 does not issue a warning.
>
> I apologize for not subscribing to the mailing list or submitting via
> GCC Buzilla.
Thanks for the apology, but it should still be reported to bugzilla
not to this list.
Your example can be reduced to
struct XYZ { };
XYZ& f(XYZ& r) { return r; }
XYZ&
get_XYZ_data()
{
XYZ result;
return f(result);
}