https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80265
Pedro Alves <palves at redhat dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |palves at redhat dot com
--- Comment #4 from Pedro Alves <palves at redhat dot com> ---
Meanwhile, maybe this could be punted to the library, making use of
__builtin_constant as a building block? I don't know what guarantees
__builtin_constant officially gives in constexpr, but at least the below
compiles fine with GCC 7, in both -std=gnu++14 and -std=gnu++17 modes.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <string>
constexpr size_t constexpr_strlen(const char* s) {
return *s ? constexpr_strlen (s + 1) + 1 : 0;
}
template <class T>
struct ce_char_traits : std::char_traits<T> {
using char_type = typename std::char_traits<T>::char_type;
static constexpr std::size_t length(const char_type* s) noexcept {
if (__builtin_constant_p (s))
return constexpr_strlen (s);
return __builtin_strlen (s);
}
};
static_assert (ce_char_traits<char>::length ("") == 0);
static_assert (ce_char_traits<char>::length ("hello") == 5);
static_assert (ce_char_traits<char>::length ("he\0llo") == 2);
static const char array[] = "foo";
static_assert (ce_char_traits<char>::length (array) == 3);