This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/80265] New: __builtin_{memcmp,memchr,strlen} are not usable in constexpr functions


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

            Bug ID: 80265
           Summary: __builtin_{memcmp,memchr,strlen} are not usable in
                    constexpr functions
           Product: gcc
           Version: 7.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

We need these functions (and wmemcmp, wmemchr and wcslen) to be usable in
constexpr functions to implement
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0426r1.html for C++17.

We only need it for C++17, but the constexpr rules are the same as C++14, and
this doesn't compile in C++14 or C++17 mode:

constexpr bool compare() {
  char s1[] = "foo";
  char s2[] = "foo";
  if (__builtin_memcmp(s1, s2, 3))
    return false;
  return true;
}

constexpr bool length() {
  char s[] = "foo";
  if (__builtin_strlen(s) != 3)
    return false;
  return true;
}

constexpr bool find() {
  char s[] = "foo";
  if (__builtin_memchr(s, 'o', 3) != s+1)
    return false;
  return true;
}

static_assert( compare(), "" );
static_assert( length(), "" );
static_assert( find(), "" );


ce.cc:23:1: error: non-constant condition for static assertion
 static_assert( compare(), "" );
 ^~~~~~~~~~~~~
ce.cc:23:23:   in constexpr expansion of ‘compare()’
ce.cc:4:23: error: ‘__builtin_memcmp(((const void*)(& s1)), ((const void*)(&
s2)), 3)’ is not a constant expression
   if (__builtin_memcmp(s1, s2, 3))
       ~~~~~~~~~~~~~~~~^~~~~~~~~~~
ce.cc:24:1: error: non-constant condition for static assertion
 static_assert( length(), "" );
 ^~~~~~~~~~~~~
ce.cc:24:22:   in constexpr expansion of ‘length()’
ce.cc:11:23: error: ‘__builtin_strlen(((const char*)(& s)))’ is not a constant
expression
   if (__builtin_strlen(s) != 3)
       ~~~~~~~~~~~~~~~~^~~
ce.cc:25:1: error: non-constant condition for static assertion
 static_assert( find(), "" );
 ^~~~~~~~~~~~~
ce.cc:25:20:   in constexpr expansion of ‘find()’
ce.cc:18:23: error: ‘__builtin_memchr(((const void*)(& s)), 111, 3)’ is not a
constant expression
   if (__builtin_memchr(s, 'o', 3) != s+1)
       ~~~~~~~~~~~~~~~~^~~~~~~~~~~

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]