Skip to content

Commit

Permalink
C++17: std::char_traits<>::{compare,length} is constexpr.
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Apr 22, 2021
1 parent 1d4199f commit 2e049b4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/fmt/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ template <typename Char> struct ansi_color_escape {
FMT_CONSTEXPR operator const Char*() 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);
}

Expand Down
22 changes: 18 additions & 4 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@
# define FMT_HAS_GXX_CXX11 0
#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__
# define FMT_NVCC __NVCC__
#else
Expand Down Expand Up @@ -415,9 +431,7 @@ template <typename Char> class basic_string_view {
the size with ``std::char_traits<Char>::length``.
\endrst
*/
#if __cplusplus >= 201703L // C++17's char_traits::length() is constexpr.
constexpr
#endif
FMT_CONSTEXPR_CHAR_TRAITS
FMT_INLINE
basic_string_view(const Char* s)
: data_(s) {
Expand Down Expand Up @@ -457,7 +471,7 @@ template <typename Char> class basic_string_view {
}

// 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_;
int result = std::char_traits<Char>::compare(data_, other.data_, str_size);
if (result == 0)
Expand Down
2 changes: 1 addition & 1 deletion include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,7 @@ FMT_CONSTEXPR OutputIt write(OutputIt out, Char value) {
}

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) {
FMT_THROW(format_error("string pointer is null"));
} else {
Expand Down

0 comments on commit 2e049b4

Please sign in to comment.