Compiling LLVM with GCC 5/6/7 and -std=c++1z fails due to use of std::char_traits constexpr


When trying to compile LLVM using GCC 5.1 => GCC 7.2 (inclusive) with -std=c++1z, using StringRef errors due to the lack of a constexpr std::char_traits::length():

…/include/llvm/ADT/StringRef.h:85:44: error: call to non-constexpr function ‘static std::size_t std::char_traits::length(const char_type*)’
return std::char_traits::length(Str);

Looking into the issue, StringRef.h uses this logic to gate against using constexpr char_traits:

#if **__cplusplus** > 201402L
return std::char_traits<char>::length(Str);

#elif ...

However for the GCC version range mentioned above, the -std=c++1z flag generates __cplusplus 201500L or higher despite char_traits not being constexpr.

I think the correct thing to do here might be to use the feature macro instead:

#if **__cpp_lib_constexpr_char_traits**

return std::char_traits<char>::length(Str);

#elif ...

Any thoughts?

  • Eric (for standard library knowledge) & Richard (for general C+±ery).

I can’t say I know much about __cpp_lib_constexpr_char_traits - got any reference on how widely supported/implemented that is?