This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/80265] __builtin_{memcmp,memchr,strlen} are not usable in constexpr functions
- From: "palves at redhat dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Mon, 10 Apr 2017 19:45:54 +0000
- Subject: [Bug c++/80265] __builtin_{memcmp,memchr,strlen} are not usable in constexpr functions
- Auto-submitted: auto-generated
- References: <bug-80265-4@http.gcc.gnu.org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80265
--- Comment #12 from Pedro Alves <palves at redhat dot com> ---
This seems to work equally well (or better):
// true if the whole string is known at compile time.
static inline constexpr bool constant_string(const char *s)
{
while (__builtin_constant_p (*s) && *s)
s++;
if (!__builtin_constant_p (*s))
return false;
return true;
}
constexpr size_t constexpr_strlen(const char* s)
{
const char *p = s;
while (*p)
p++;
return p - s;
}
and then use it in ce_char_traits like:
static constexpr std::size_t length(const char_type* s) noexcept
{
if (constant_string(s))
return constexpr_strlen (s);
return __builtin_strlen (s);
}
I.e., decouple the "is the whole string constant" from the strlen algorithm.
This should make it easier to reuse the "is compile-time string" in other
compile-time algorithms, though the previous version in comment #11 potentially
optimized the computing the length of the constant prefix part of the string.
(which is probably not a common use case to aim for anyway.)
Sorry for the constant spam...