public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
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	[thread overview]
Message-ID: <200106181618.f5IGIUZ17063@odie.andypo.net> (raw)

>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:


             reply	other threads:[~2001-06-18  9:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-18  9:26 andrewp [this message]
2001-08-11 13:19 gdr
2002-03-18  6:02 jason
2002-03-21  0:26 Gabriel Dos Reis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200106181618.f5IGIUZ17063@odie.andypo.net \
    --to=andrewp@andypo.net \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).