[Bug libstdc++/99181] char_traits<char> (and thus string_view) compares strings differently in constexpr and non-constexpr contexts
cvs-commit at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Thu Apr 22 16:51:12 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99181
--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-8 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:
https://gcc.gnu.org/g:21a7edf3a0cea656f2de834ea8320870496d5a2f
commit r8-10889-g21a7edf3a0cea656f2de834ea8320870496d5a2f
Author: Jakub Jelinek <jakub@redhat.com>
Date: Tue Feb 23 09:30:18 2021 +0100
libstdc++: Fix up constexpr std::char_traits<char>::compare [PR99181]
Because of LWG 467, std::char_traits<char>::lt compares the values
cast to unsigned char rather than char, so even when char is signed
we get unsigned comparision. std::char_traits<char>::compare uses
__builtin_memcmp and that works the same, but during constexpr evaluation
we were calling __gnu_cxx::char_traits<char_type>::compare. As
char_traits::lt is not virtual, __gnu_cxx::char_traits<char_type>::compare
used __gnu_cxx::char_traits<char_type>::lt rather than
std::char_traits<char>::lt and thus compared chars as signed if char is
signed.
This change fixes it by inlining __gnu_cxx::char_traits<char_type>::compare
into std::char_traits<char>::compare by hand, so that it calls the right
lt method.
2021-02-23 Jakub Jelinek <jakub@redhat.com>
PR libstdc++/99181
* include/bits/char_traits.h (char_traits<char>::compare): For
constexpr evaluation don't call
__gnu_cxx::char_traits<char_type>::compare but do the comparison
loop
directly.
* testsuite/21_strings/char_traits/requirements/char/99181.cc: New
test.
(cherry picked from commit 311c57f6d8f285d69e44bf94152c753900cb1a0a)
More information about the Gcc-bugs
mailing list