------- 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" <[EMAIL PROTECTED]> 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" <[EMAIL PROTECTED]> 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