public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "jakub at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/99181] char_traits<char> (and thus string_view) compares strings differently in constexpr and non-constexpr contexts Date: Mon, 22 Feb 2021 09:43:31 +0000 [thread overview] Message-ID: <bug-99181-4-K8jrFM3G9c@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-99181-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99181 --- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> --- static _GLIBCXX_CONSTEXPR bool lt(const char_type& __c1, const char_type& __c2) _GLIBCXX_NOEXCEPT { // LWG 467. return (static_cast<unsigned char>(__c1) < static_cast<unsigned char>(__c2)); } static _GLIBCXX17_CONSTEXPR int compare(const char_type* __s1, const char_type* __s2, size_t __n) { if (__n == 0) return 0; #if __cplusplus >= 201703L if (__builtin_constant_p(__n) && __constant_char_array_p(__s1, __n) && __constant_char_array_p(__s2, __n)) return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n); #endif return __builtin_memcmp(__s1, __s2, __n); } Seems the std::char_traits<char>::lt specialization implements LWG467, and so does std::char_traits<char>::compare when not in constant expression evaluation, but the C++17 case above will call __gnu_cxx::char_traits<char_type>::compare which doesn't know about LWG 467. --- libstdc++-v3/include/bits/char_traits.h.jj 2021-01-04 10:26:03.558953845 +0100 +++ libstdc++-v3/include/bits/char_traits.h 2021-02-22 10:42:08.197199781 +0100 @@ -349,7 +349,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__builtin_constant_p(__n) && __constant_char_array_p(__s1, __n) && __constant_char_array_p(__s2, __n)) - return __gnu_cxx::char_traits<char_type>::compare(__s1, __s2, __n); + { + for (std::size_t __i = 0; __i < __n; ++__i) + if (lt(__s1[__i], __s2[__i])) + return -1; + else if (lt(__s2[__i], __s1[__i])) + return 1; + return 0; + } #endif return __builtin_memcmp(__s1, __s2, __n); } seems to fix this, but not sure if there isn't a better way.
next prev parent reply other threads:[~2021-02-22 9:43 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-02-20 15:47 [Bug libstdc++/99181] New: " richardpku at gmail dot com 2021-02-22 9:43 ` jakub at gcc dot gnu.org [this message] 2021-02-22 14:27 ` [Bug libstdc++/99181] " jakub at gcc dot gnu.org 2021-02-22 14:42 ` redi at gcc dot gnu.org 2021-02-23 8:37 ` cvs-commit at gcc dot gnu.org 2021-02-23 9:12 ` jakub at gcc dot gnu.org 2021-02-23 10:46 ` redi at gcc dot gnu.org 2021-03-19 23:29 ` cvs-commit at gcc dot gnu.org 2021-04-20 23:32 ` cvs-commit at gcc dot gnu.org 2021-04-22 16:51 ` cvs-commit at gcc dot gnu.org 2021-04-22 17:11 ` jakub at gcc dot gnu.org 2021-09-11 14:23 ` pinskia at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-99181-4-K8jrFM3G9c@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).