From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1924 invoked by alias); 2 Jan 2005 00:43:52 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 1916 invoked by alias); 2 Jan 2005 00:43:50 -0000 Date: Sun, 02 Jan 2005 00:43:00 -0000 Message-ID: <20050102004350.1915.qmail@sourceware.org> From: "gdr at integrable-solutions dot net" To: gcc-bugs@gcc.gnu.org In-Reply-To: <20041230105911.19199.lars@trolltech.com> References: <20041230105911.19199.lars@trolltech.com> Reply-To: gcc-bugzilla@gcc.gnu.org Subject: [Bug c++/19199] [3.3/3.4/4.0 Regression] Wrong warning about returning a reference to a temporary X-Bugzilla-Reason: CC X-SW-Source: 2005-01/txt/msg00090.txt.bz2 List-Id: ------- 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" 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" 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 enum Foo { A, B }; template 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