unjustified warning when returning reference thru conditional expression

Ulrich Lauther ulrich.lauther@mchp.siemens.de
Thu Sep 20 01:50:00 GMT 2001


Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/specs
Configured with: /usr/local/src/gcc-3.0.1/configure --enable-languages=c,c++
Thread model: single
gcc version 3.0.1
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.0.1/cc1plus -v -D__GNUC__=3 -D__GNUC_MINOR__=0 -D__GNUC_PATCHLEVEL__=1 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -D_GNU_SOURCE -Acpu=i386 -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i686__ -D__tune_pentiumpro__ test2.C -D__GNUG__=3 -D__GXX_DEPRECATED -D__EXCEPTIONS -D__GXX_ABI_VERSION=100 -quiet -dumpbase test2.C -version -o /tmp/cc8Vx7bD.s
GNU CPP version 3.0.1 (cpplib) (i386 Linux/ELF)
GNU C++ version 3.0.1 (i686-pc-linux-gnu)
	compiled by GNU C version 3.0.1.

Source Code (test2.C):

class Foo {
	int  a;
	int* b;

	const int& a_or_b1(bool which) const {
		return which ? a : *b;
	const int& a_or_b2(bool which) const {
		if (which) return a; else return *b;

void test() {
	Foo foo;
	int x = foo.a_or_b1(true);
	int y = foo.a_or_b2(true);

compile-statement: gcc -c test2.C

test2.C: In member function `const int& Foo::a_or_b1(bool) const':
test2.C:7: warning: returning reference to temporary

The warning is believed to be unjustified, as there is no need to create a

Kind regards,

Ulrich Lauther          ph: +49 89 636 48834 fx: ... 636 42284
Siemens CT SE 6         Internet: Ulrich.Lauther@mchp.siemens.de

More information about the Gcc-bugs mailing list