Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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