C++17: std::char_traits<>::{compare,length} is constexpr. (#2246)

This commit is contained in:
Vladislav Shchapov 2021-04-23 18:11:34 +05:00 committed by GitHub
parent 841aad95b4
commit 128f007b25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 6 deletions

View File

@ -432,7 +432,7 @@ template <typename Char> struct ansi_color_escape {
FMT_CONSTEXPR operator const Char*() const FMT_NOEXCEPT { return buffer; } FMT_CONSTEXPR operator const Char*() const FMT_NOEXCEPT { return buffer; }
FMT_CONSTEXPR const Char* begin() const FMT_NOEXCEPT { return buffer; } FMT_CONSTEXPR const Char* begin() const FMT_NOEXCEPT { return buffer; }
FMT_CONSTEXPR const Char* end() const FMT_NOEXCEPT { FMT_CONSTEXPR_CHAR_TRAITS const Char* end() const FMT_NOEXCEPT {
return buffer + std::char_traits<Char>::length(buffer); return buffer + std::char_traits<Char>::length(buffer);
} }

View File

@ -43,6 +43,22 @@
# define FMT_HAS_GXX_CXX11 0 # define FMT_HAS_GXX_CXX11 0
#endif #endif
// Check if constexpr std::char_traits<>::compare,length is supported.
// libstdc++: present on GCC 7 and newer and __cplusplus >= 201703L
// MSVC, libc++: always if __cplusplus >= 201703L
// NOTE: FMT_GCC_VERSION - is not libstdc++ version.
// _GLIBCXX_RELEASE - is present in GCC 7 libstdc++ and newer.
#if __cplusplus >= 201703L
# ifndef __GLIBCXX__
# define FMT_CONSTEXPR_CHAR_TRAITS constexpr
# elif defined(_GLIBCXX_RELEASE) && _GLIBCXX_RELEASE >= 7
# define FMT_CONSTEXPR_CHAR_TRAITS constexpr
# endif
#endif
#ifndef FMT_CONSTEXPR_CHAR_TRAITS
# define FMT_CONSTEXPR_CHAR_TRAITS
#endif
#ifdef __NVCC__ #ifdef __NVCC__
# define FMT_NVCC __NVCC__ # define FMT_NVCC __NVCC__
#else #else
@ -415,9 +431,7 @@ template <typename Char> class basic_string_view {
the size with ``std::char_traits<Char>::length``. the size with ``std::char_traits<Char>::length``.
\endrst \endrst
*/ */
#if __cplusplus >= 201703L // C++17's char_traits::length() is constexpr. FMT_CONSTEXPR_CHAR_TRAITS
constexpr
#endif
FMT_INLINE FMT_INLINE
basic_string_view(const Char* s) basic_string_view(const Char* s)
: data_(s) { : data_(s) {
@ -457,7 +471,7 @@ template <typename Char> class basic_string_view {
} }
// Lexicographically compare this string reference to other. // Lexicographically compare this string reference to other.
int compare(basic_string_view other) const { FMT_CONSTEXPR_CHAR_TRAITS int compare(basic_string_view other) const {
size_t str_size = size_ < other.size_ ? size_ : other.size_; size_t str_size = size_ < other.size_ ? size_ : other.size_;
int result = std::char_traits<Char>::compare(data_, other.data_, str_size); int result = std::char_traits<Char>::compare(data_, other.data_, str_size);
if (result == 0) if (result == 0)

View File

@ -2130,7 +2130,7 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, Char value) {
} }
template <typename Char, typename OutputIt> template <typename Char, typename OutputIt>
FMT_CONSTEXPR OutputIt write(OutputIt out, const Char* value) { FMT_CONSTEXPR_CHAR_TRAITS OutputIt write(OutputIt out, const Char* value) {
if (!value) { if (!value) {
FMT_THROW(format_error("string pointer is null")); FMT_THROW(format_error("string pointer is null"));
} else { } else {