public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "richardpku at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/99181] New: char_traits<char> (and thus string_view) compares strings differently in constexpr and non-constexpr contexts
Date: Sat, 20 Feb 2021 15:47:57 +0000	[thread overview]
Message-ID: <bug-99181-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2021-02-20 15:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-20 15:47 richardpku at gmail dot com [this message]
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

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@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: link
Be 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).