From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E9A9E3858D35; Wed, 4 Jan 2023 13:15:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9A9E3858D35 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1672838126; bh=iNkfgVo1PBTiCTBq/k5Gk0aNF/UmqrzrzvMd3poozqA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ecNUFsdo+gA2B4xa4Umm/i0QcEdzayIpODzpfHRWuvRoTB5CUHnHhNkUsGky4gLdK KO+t4LeFimrQEiFGaQ8Os4LGmLLLxLvuv8Kj77M3JTo3zlentinBs88t5705EUrMwO 9SLP+Sq9ZLbSCxo4F2lbyCCUw9zX3tr7HBqSOcGs= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/108243] Missed optimization for static const std::string_view(const char*) Date: Wed, 04 Jan 2023 13:15:26 +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: 11.3.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108243 --- Comment #2 from Jonathan Wakely --- Reduced: struct traits { static constexpr unsigned long length(const char* s) { if (__builtin_is_constant_evaluated()) { unsigned long n =3D 0; while (*s++) ++n; return n; } return __builtin_strlen(s); } }; struct string_view { constexpr string_view(const char* s) : str(s), len(traits::length(s)) { } unsigned long size() const { return len; } const char* str; unsigned long len; }; int main() { static const string_view foo("bar"); return foo.size(); } GCC should really be able to optimize this.=