This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/19199] [3.3/3.4/4.0 Regression] Wrong warning about returning a reference to a temporary
- From: "gdr at integrable-solutions dot net" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 2 Jan 2005 00:43:50 -0000
- Subject: [Bug c++/19199] [3.3/3.4/4.0 Regression] Wrong warning about returning a reference to a temporary
- References: <20041230105911.19199.lars@trolltech.com>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From gdr at integrable-solutions dot net 2005-01-02 00:43 -------
Subject: Re: [3.3/3.4/4.0 Regression] Wrong warning about returning a reference to a temporary
"pinskia at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes:
| (In reply to comment #2)
| > Why would this cause wrong code? It's just a cast from an enum to an int,
| > I don't see what's wrong with that except that it's ugly.
| >
| > You have to actually show that this causes wrong code, not just make your
| > usual exaggerated claims ;-)
|
| Ok, the reason why it is wrong code is because we return an address to a temporary which we can
| overwrite. Yes this is wrong code. The diagnostic is just an indication that it will be wrong code.
"rth at gcc dot gnu dot org" <gcc-bugzilla@gcc.gnu.org> writes:
| When references are involved, like this, reduction to MAX_EXPR is wrong to
| begin with. The cast is only an additional symptom.
Both Andrew and RTH are obviously right. This is wrong-code
generation. As a "proof", I've slighlty modified the testcase to
include an assertion on the address of the object that realizes the
min. It fails, as can be expected from the warning.
Here we go.
% cat 19199.C && g++ 19199.C -o 19199 && ./19199
#include <assert.h>
enum Foo { A, B };
template<typename T> const T &qMin(const T &a, const T &b)
{ return a < b ? a : b; }
int main(int, char **)
{
Foo f = A;
Foo g = B;
const Foo& h = qMin(f, g);
assert(&h == &f || &h == &g);
return 0;
}
19199.C: In function 'const T& qMin(const T&, const T&) [with T = Foo]':
19199.C:12: instantiated from here
19199.C:6: warning: returning reference to temporary
19199: 19199.C:13: int main(int, char**): Assertion `&h == &f || &h == &g' failed.
zsh: 10279 abort ./19199
-- Gaby
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19199