Warning: returning reference to temporary
Dr. Andreas Stuebinger
andreas.stuebinger@mchp.siemens.de
Wed Mar 8 08:14:00 GMT 2000
consider the following C++ snippet:
-----------------------------------------------------
#define REPBITS 32
class foo {
public:
unsigned short set_width;
unsigned long* pbits;
unsigned long bits;
unsigned long& word() {
return set_width <= REPBITS ? bits : *(pbits);
}
const unsigned long& word() const {
return set_width <= REPBITS ? bits : *(pbits);
}
};
-----------------------------------------------------
Compiling it with the latest egcs-20000306 version on RedHat 6.0 results in:
460> /opt/egcs-20000306/bin/g++ -v -c foo.C
Reading specs from /opt/egcs-20000306/lib/gcc-lib/i686-pc-linux-gnu/2.96/specs
gcc version 2.96 20000306 (experimental)
/opt/egcs-20000306/lib/gcc-lib/i686-pc-linux-gnu/2.96/cpp -lang-c++ -v -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=96 -D__GNUC_PATCHLEVEL__=0 -D__cplusplus -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -D__tune_pentiumpro__ foo.C /tmp/cclFDDnL.ii
GNU CPP version 2.96 20000306 (experimental) (cpplib)
(i386 Linux/ELF)
ignoring nonexistent directory `/usr/local/include'
#include "..." search starts here:
#include <...> search starts here:
/opt/egcs-20000306/include/g++-3
/opt/egcs-20000306/i686-pc-linux-gnu/include
/opt/egcs-20000306/lib/gcc-lib/i686-pc-linux-gnu/2.96/include
/usr/include
End of search list.
/opt/egcs-20000306/lib/gcc-lib/i686-pc-linux-gnu/2.96/cc1plus /tmp/cclFDDnL.ii -quiet -dumpbase foo.cc -version -o /tmp/ccW5XMEr.s
GNU C++ version 2.96 20000306 (experimental) (i686-pc-linux-gnu) compiled by GNU C version 2.96 20000306 (experimental).
foo.C: In method `const long unsigned int &foo::word () const':
foo.C:19: warning: returning reference to temporary
as -V -Qy -o foo.o /tmp/ccW5XMEr.s
GNU assembler version 2.9.1 (i386-redhat-linux), using BFD version 2.9.1.0.23
Why the warning about returning reference to temporary? If the const
version is changed to
if(set_width <= REPBITS)
return bits;
else
return *(pbits);
no warning is emitted.
--
MfG,
Andreas Stübinger
--------------------------------------------------------------------------
Dr. Andreas Stübinger Email: Andreas.Stuebinger@mchp.siemens.de
Siemens AG, ZT SE 4 Phone: ++49-89/636-51338
D-81730 München, Germany Fax: ++49-89/636-42284
More information about the Gcc-bugs
mailing list