public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/99181] New: char_traits<char> (and thus string_view) compares strings differently in constexpr and non-constexpr contexts
@ 2021-02-20 15:47 richardpku at gmail dot com
  2021-02-22  9:43 ` [Bug libstdc++/99181] " jakub at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: richardpku at gmail dot com @ 2021-02-20 15:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99181

            Bug ID: 99181
           Summary: char_traits<char> (and thus string_view) compares
                    strings differently in constexpr and non-constexpr
                    contexts
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: richardpku at gmail dot com
  Target Milestone: ---

Minimal program to produce bug (run it on a platform where char is a signed
type, such as i386/x86-64):

/tmp % cat a.cpp
#include <string_view>
#include <iostream>

using namespace std;

int main() {
        // constexpr
        constexpr bool i = ("\xff"sv > "aaa"sv);
        cout << i << ",";

        // not constexpr
        auto a = "\xff"sv, b = "aaa"sv;
        cout << (a > b) << endl;

        return 0;
}
/tmp % g++ -std=gnu++2a a.cpp && ./a.out
0,1

The expected result is "1,1".


In a non-constexpr context, std::char_traits<char>::compare invokes
__builtin_memcmp, which is required by C standard to interpret characters as
unsigned char.

In a constexpr context, however, std::char_traits<char>::compare invokes
__gnu_cxx::char_traits<char>::compare, which in turn calls
__gnu_cxx::char_traits<char>::lt to compare chars.
__gnu_cxx::char_traits<char>::lt (unlike std::char_traits<char>::lt) is not
specialized to compare chars as unsigned char.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-09-11 14:23 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-20 15:47 [Bug libstdc++/99181] New: char_traits<char> (and thus string_view) compares strings differently in constexpr and non-constexpr contexts richardpku at gmail dot com
2021-02-22  9:43 ` [Bug libstdc++/99181] " jakub at gcc dot gnu.org
2021-02-22 14:27 ` 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

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).