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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Reduced:

struct traits
{
  static constexpr unsigned long length(const char* s)
  {
    if (__builtin_is_constant_evaluated())
    {
      unsigned long n = 0;
      while (*s++)
        ++n;
      return n;
    }
    return __builtin_strlen(s);
  }
};

struct string_view
{
  constexpr
  string_view(const char* s) : str(s), len(traits::length(s)) { }

  unsigned long size() const { return len; }

  const char* str;
  unsigned long len;
};

int main()
{
  static const string_view foo("bar");
  return foo.size();
}


GCC should really be able to optimize this.

Reply via email to