From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11362 invoked by alias); 1 Oct 2015 12:42:55 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 11326 invoked by uid 48); 1 Oct 2015 12:42:51 -0000 From: "frankhb1989 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/67795] Wrong code generated for conditional expression with cast Date: Thu, 01 Oct 2015 12:42:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: frankhb1989 at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-10/txt/msg00032.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D67795 --- Comment #3 from frankhb1989 at gmail dot com --- (In reply to Markus Trippelsdorf from comment #1) > gcc even warns: >=20 > t.cpp: In function =E2=80=98std::experimental::fundamentals_v1::string_vi= ew& > erase_left(size_t, std::experimental::fundamentals_v1::string_view&)=E2= =80=99: > t.cpp:9:73: warning: function returns address of local variable > [-Wreturn-local-addr] > return static_cast(n !=3D 0 ? (s.remove_prefix(n), s) := s); > ^ And if the diagnostics exist, they are definitely wrong. First, here 's' is a local variable (in sense of C++ terms) indeed, but thi= s is perfectly valid. Note it is a _parameter_ referencing to an object whose lifetime is unknown in the function/block scopes. This should not trigger s= uch warnings. Otherwise the identity functions for pointers/references are alwa= ys invalid. That's absurd. Second, the "local variable" wording is somewhat misleading. Returning reference to a local variable is generally wrong because the variable is an _automatic object_ and it would be no sense to use the return value to reference an end-of-life object which would cause undefined behavior. Anyway, this is just not the case here. >>From gcc-bugs-return-498478-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Thu Oct 01 12:48:18 2015 Return-Path: Delivered-To: listarch-gcc-bugs@gcc.gnu.org Received: (qmail 22977 invoked by alias); 1 Oct 2015 12:48:13 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Delivered-To: mailing list gcc-bugs@gcc.gnu.org Received: (qmail 22903 invoked by uid 48); 1 Oct 2015 12:48:08 -0000 From: "trippels at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/67795] Wrong code generated for conditional expression with cast Date: Thu, 01 Oct 2015 12:48:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: trippels at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_status resolution Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-10/txt/msg00033.txt.bz2 Content-length: 492 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67795 Markus Trippelsdorf changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |RESOLVED Resolution|--- |INVALID --- Comment #4 from Markus Trippelsdorf --- Return by value if you want to avoid undefined behavior.