This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++/3230: False warning for 'returning reference to temporary'



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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]