https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78156
Bug ID: 78156 Summary: constexpr basic_string_view::basic_string_view(const charT *) calls non-constexpr char_traits::length Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: suokkos at gmail dot com Target Milestone: --- Simple test case fails to compile: #if __has_include("string_view") #include <string_view> #elif __has_include("experimental/string_view") #include <experimental/string_view> namespace std { using namespace experimental; } #endif constexpr std::string_view foo("bar"); Result with /usr/lib/gcc-snapshot/bin/g++ -c -std=c++17 -Wall test.cc -o test.o /usr/lib/gcc-snapshot/bin/g++ --version g++ (Ubuntu 20161006-1ubuntu1) 7.0.0 20161006 (experimental) [trunk revision 240826] In file included from test.cc:2:0: test.cc:11:37: in constexpr expansion of 'std::basic_string_view<char>(((const char*)"bar"))' /usr/lib/gcc-snapshot/include/c++/7.0.0/string_view:100:58: error: call to non-constexpr function 'static std::size_t std::char_traits<char>::length(const char_type*)' : _M_len{__str == nullptr ? 0 : traits_type::length(__str)}, ~~~~~~~~~~~~~~~~~~~^~~~~~~ Related code locations: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/experimental/string_view#L110 https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/char_traits.h#L106 https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/char_traits.h#L265 Possible fix that would require changes to the standard would changing std::char_traits::length to be constexpr. I tested that change to my headers and it works with small changes for this case. I don't know if adding constexpr will cause any other problems except preventing std::char_traits::length from calling strlen. But latest version is already using __builtin_strlen. Does __builtin_strlen pass constexpr test? Yes. Both gcc and clang managed to compile constexpr with builtin_strlen. Alternative workarounds would be duplicating std::char_trait implementation with constexpr alternatives that can be used to make string_view provide constexpr constructor.