public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c++/3230: False warning for 'returning reference to temporary'
@ 2002-03-18 6:02 jason
0 siblings, 0 replies; 4+ messages in thread
From: jason @ 2002-03-18 6:02 UTC (permalink / raw)
To: andrewp, gcc-bugs, gcc-prs, gdr, jason
Synopsis: False warning for 'returning reference to temporary'
Responsible-Changed-From-To: gdr->jason
Responsible-Changed-By: jason
Responsible-Changed-When: Mon Mar 18 06:02:19 2002
Responsible-Changed-Why:
mine
State-Changed-From-To: analyzed->closed
State-Changed-By: jason
State-Changed-When: Mon Mar 18 06:02:19 2002
State-Changed-Why:
This is not a bug in the compiler. The standard (5.16) says that the result of your ?: expression is an rvalue, because the two options do not have the same type. As a result, the return value binds to that rvalue, which is a local temporary. The EDG compiler gives the same warning.
You may feel that this is a bug in the standard, and I'd be inclined to agree. But until the standard is fixed, the compiler will continue to work this way. If you'd like to submit a defect report against the standard, the place for that is comp.std.c++.
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=3230
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: c++/3230: False warning for 'returning reference to temporary'
@ 2002-03-21 0:26 Gabriel Dos Reis
0 siblings, 0 replies; 4+ messages in thread
From: Gabriel Dos Reis @ 2002-03-21 0:26 UTC (permalink / raw)
To: jason; +Cc: gcc-prs
The following reply was made to PR c++/3230; it has been noted by GNATS.
From: Gabriel Dos Reis <gdr@codesourcery.com>
To: jason@gcc.gnu.org
Cc: andrewp@andypo.net, gcc-bugs@gcc.gnu.org, gdr@gcc.gnu.org,
gcc-gnats@gcc.gnu.org
Subject: Re: c++/3230: False warning for 'returning reference to temporary'
Date: 21 Mar 2002 09:17:51 +0100
jason@gcc.gnu.org writes:
| Synopsis: False warning for 'returning reference to temporary'
|
| Responsible-Changed-From-To: gdr->jason
| Responsible-Changed-By: jason
| Responsible-Changed-When: Mon Mar 18 06:02:19 2002
| Responsible-Changed-Why:
| mine
| State-Changed-From-To: analyzed->closed
| State-Changed-By: jason
| State-Changed-When: Mon Mar 18 06:02:19 2002
| State-Changed-Why:
| This is not a bug in the compiler. The standard (5.16) says that the result of your ?: expression is an rvalue, because the two options do not have the same type. As a result, the return value binds to that rvalue, which is a local temporary. The EDG compiler gives the same warning.
OK.
| You may feel that this is a bug in the standard, and I'd be
| inclined to agree. But until the standard is fixed, the compiler will
| continue to work this way.
I'll raise the issue on the core group refelector.
-- Gaby
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: c++/3230: False warning for 'returning reference to temporary'
@ 2001-08-11 13:19 gdr
0 siblings, 0 replies; 4+ messages in thread
From: gdr @ 2001-08-11 13:19 UTC (permalink / raw)
To: andrewp, gcc-bugs, gcc-prs, gdr, nobody
Synopsis: False warning for 'returning reference to temporary'
Responsible-Changed-From-To: unassigned->gdr
Responsible-Changed-By: gdr
Responsible-Changed-When: Sat Aug 11 13:19:49 2001
Responsible-Changed-Why:
Analyzed.
State-Changed-From-To: open->analyzed
State-Changed-By: gdr
State-Changed-When: Sat Aug 11 13:19:49 2001
State-Changed-Why:
Confirmed as a bug. A regression over 2.95.2
http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view&pr=3230&database=gcc
^ permalink raw reply [flat|nested] 4+ messages in thread
* c++/3230: False warning for 'returning reference to temporary'
@ 2001-06-18 9:26 andrewp
0 siblings, 0 replies; 4+ messages in thread
From: andrewp @ 2001-06-18 9:26 UTC (permalink / raw)
To: gcc-gnats
>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:
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2002-03-21 8:26 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-18 6:02 c++/3230: False warning for 'returning reference to temporary' jason
-- strict thread matches above, loose matches on Subject: below --
2002-03-21 0:26 Gabriel Dos Reis
2001-08-11 13:19 gdr
2001-06-18 9:26 andrewp
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).