From mboxrd@z Thu Jan 1 00:00:00 1970 From: andrewp@andypo.net To: gcc-gnats@gcc.gnu.org Subject: c++/3230: False warning for 'returning reference to temporary' Date: Mon, 18 Jun 2001 09:26:00 -0000 Message-id: <200106181618.f5IGIUZ17063@odie.andypo.net> X-SW-Source: 2001-06/msg00738.html List-Id: >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: