This is the mail archive of the
gcc-prs@gcc.gnu.org
mailing list for the GCC project.
c++/3230: False warning for 'returning reference to temporary'
- To: gcc-gnats at gcc dot gnu dot org
- Subject: c++/3230: False warning for 'returning reference to temporary'
- From: andrewp at andypo dot net
- Date: Mon, 18 Jun 2001 17:18:30 +0100
>Number: 3230
>Category: c++
>Synopsis: False warning for 'returning reference to temporary'
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: unassigned
>State: open
>Class: rejects-legal
>Submitter-Id: net
>Arrival-Date: Mon Jun 18 09:26:06 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: Andrew Pollard
>Release: 3.0 20010616 (prerelease)
>Organization:
>Environment:
System: Linux odie.andypo.net 2.2.19-7.0.1smp #1 SMP Tue Apr 10 01:46:39 EDT 2001 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc-3_0-branch/configure --prefix=/usr/local/gcc-3.0-i686-pc-linux-gnu --with-gnu-as --with-gnu-ld --enable-threads=posix --enable-version-specific-runtime-libs --enable-languages=c++,java --with-dwarf2 --enable-long-long=yes --enable-shared
>Description:
Gcc-3.0 generates an incorrect warning about returning a reference to a temporary when a ?: expression is used directly in a return statement. If another temporary is used, no warning is given.
>How-To-Repeat:
a.cc:
------------------------------------------------------------------------------
const int* bar();
const int&
foo1()
{
static int empty;
const int* x = bar();
return (x ? *x : empty);
}
const int&
foo2()
{
static int empty;
const int* x = bar();
const int& r = (x ? *x : empty);
return (r);
}
------------------------------------------------------------------------------
% /usr/local/gcc-3.0-i686-pc-linux-gnu/bin/g++ -c a.cc
a.cc: In function `const int& foo1()':
a.cc:8: warning: returning reference to temporary
Whereas foo2() compiles successfully.
gcc-2.95.3 and egcs-1.1.2 do not give this warning.
>Fix:
Workaround simple, just use another temporary to store the returned result and return that instead
const int& r = (x ? *x : empty);
return (r);
instead of
return (x ? *x : empty);
>Release-Note:
>Audit-Trail:
>Unformatted: