[Bug c++/108243] Missed optimization for static const std::string_view(const char*)
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Jan 4 13:05:35 GMT 2023
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108243
--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is caused by a change in libstdc++ headers:
@@ -230,9 +230,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
static _GLIBCXX_ALWAYS_INLINE constexpr bool
__constant_string_p(const _CharT* __s)
{
+#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
+ (void) __s;
+ // In constexpr contexts all strings should be constant.
+ return __builtin_is_constant_evaluated();
+#else
while (__builtin_constant_p(*__s) && *__s)
__s++;
return __builtin_constant_p(*__s);
+#endif
}
/**
That affects the definition of std::char_traits<char>::length:
static constexpr size_t
length(const char_type* __s)
{
if (__constant_string_p(__s))
return __gnu_cxx::char_traits<char_type>::length(__s);
return __builtin_strlen(__s);
}
So since GCC 9.1.0 we only treat the length as a constant expression if the
string_view object is constexpr, otherwise we call strlen, which requires the
object to exist.
I think the compiler _should_ be able to optimize this case anyway, but maybe
we need to partially revert the libstdc++ changes.
More information about the Gcc-bugs
mailing list