From 39adcc30f8c9019b3a2f5119eac487e51a77af6f Mon Sep 17 00:00:00 2001 From: Alexey Ochapov Date: Tue, 24 Nov 2020 00:10:34 +0300 Subject: [PATCH] apply requested changes --- include/fmt/compile.h | 3 +-- include/fmt/core.h | 21 +++++++++++++-------- include/fmt/format.h | 33 ++++++++++++++++----------------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 2bfb202e..0f7a9005 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -664,8 +664,7 @@ constexpr OutputIt format_to(OutputIt out, const CompiledFormat& cf, template ::value)> -FMT_CONSTEXPR14 OutputIt format_to(OutputIt out, const S&, - const Args&... args) { +FMT_CONSTEXPR OutputIt format_to(OutputIt out, const S&, const Args&... args) { constexpr auto compiled = detail::compile(S()); return format_to(out, compiled, args...); } diff --git a/include/fmt/core.h b/include/fmt/core.h index e1eb861c..613118c4 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -96,18 +96,15 @@ # define FMT_CONSTEXPR_DECL #endif -#if __cplusplus >= 201402L -# define FMT_CONSTEXPR14 constexpr -#else -# define FMT_CONSTEXPR14 -#endif - #if __cplusplus >= 202002L # define FMT_CONSTEXPR20 constexpr -# define FMT_IS_CONSTANT_EVALUATED std::is_constant_evaluated() +# define FMT_HAS_IS_CONSTANT_EVALUATED 1 #else # define FMT_CONSTEXPR20 -# define FMT_IS_CONSTANT_EVALUATED false +#endif + +#ifndef FMT_HAS_IS_CONSTANT_EVALUATED +# define FMT_HAS_IS_CONSTANT_EVALUATED 0 #endif #ifndef FMT_OVERRIDE @@ -293,6 +290,14 @@ struct monostate {}; namespace detail { +constexpr bool is_constant_evaluated() FMT_DETECTED_NOEXCEPT { +#if FMT_HAS_IS_CONSTANT_EVALUATED + return std::is_constant_evaluated(); +#else + return false; +#endif +} + // A helper function to suppress "conditional expression is constant" warnings. template constexpr T const_check(T value) { return value; } diff --git a/include/fmt/format.h b/include/fmt/format.h index 626bf7f2..5292ee4e 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -620,7 +620,7 @@ using needs_conversion = bool_constant< template ::value)> -FMT_CONSTEXPR14 OutputIt copy_str(InputIt begin, InputIt end, OutputIt it) { +FMT_CONSTEXPR OutputIt copy_str(InputIt begin, InputIt end, OutputIt it) { while (begin != end) *it++ = *begin++; return it; } @@ -629,7 +629,7 @@ template ::value)> inline FMT_CONSTEXPR20 OutChar* copy_str(InputIt begin, InputIt end, OutChar* out) { - if (FMT_IS_CONSTANT_EVALUATED) { + if (is_constant_evaluated()) { return copy_str(begin, end, out); } return std::uninitialized_copy(begin, end, out); @@ -980,7 +980,7 @@ FMT_EXTERN template struct basic_data; // This is a struct rather than an alias to avoid shadowing warnings in gcc. struct data : basic_data<> {}; -template inline FMT_CONSTEXPR14 int count_digits_trivial(T n) { +template inline FMT_CONSTEXPR int count_digits_trivial(T n) { int count = 1; for (;;) { // Integer division is slow so do it for a group of four digits instead @@ -999,7 +999,7 @@ template inline FMT_CONSTEXPR14 int count_digits_trivial(T n) { // Returns the number of decimal digits in n. Leading zeros are not counted // except for n == 0 in which case count_digits returns 1. inline FMT_CONSTEXPR20 int count_digits(uint64_t n) { - if (FMT_IS_CONSTANT_EVALUATED) { + if (is_constant_evaluated()) { return count_digits_trivial(n); } // https://github.com/fmtlib/format-benchmark/blob/master/digits10 @@ -1008,13 +1008,13 @@ inline FMT_CONSTEXPR20 int count_digits(uint64_t n) { } #else // Fallback version of count_digits used when __builtin_clz is not available. -inline FMT_CONSTEXPR14 int count_digits(uint64_t n) { +inline FMT_CONSTEXPR int count_digits(uint64_t n) { return count_digits_trivial(n); } #endif #if FMT_USE_INT128 -inline FMT_CONSTEXPR14 int count_digits(uint128_t n) { +inline FMT_CONSTEXPR int count_digits(uint128_t n) { int count = 1; for (;;) { // Integer division is slow so do it for a group of four digits instead @@ -1032,7 +1032,7 @@ inline FMT_CONSTEXPR14 int count_digits(uint128_t n) { // Counts the number of digits in n. BITS = log2(radix). template -inline FMT_CONSTEXPR14 int count_digits(UInt n) { +inline FMT_CONSTEXPR int count_digits(UInt n) { int num_digits = 0; do { ++num_digits; @@ -1053,7 +1053,7 @@ template <> int count_digits<4>(detail::fallback_uintptr n); #if defined(FMT_BUILTIN_CLZ) // Optional version of count_digits for better performance on 32-bit platforms. inline FMT_CONSTEXPR20 int count_digits(uint32_t n) { - if (FMT_IS_CONSTANT_EVALUATED) { + if (is_constant_evaluated()) { return count_digits_trivial(n); } auto t = bsr2log10(FMT_BUILTIN_CLZ(n | 1) ^ 31); @@ -1097,20 +1097,19 @@ constexpr bool equal2(const Char* lhs, const char* rhs) { return lhs[0] == rhs[0] && lhs[1] == rhs[1]; } inline FMT_CONSTEXPR20 bool equal2(const char* lhs, const char* rhs) { - if (FMT_IS_CONSTANT_EVALUATED) { + if (is_constant_evaluated()) { return equal2(lhs, rhs); } return memcmp(lhs, rhs, 2) == 0; } // Copies two characters from src to dst. -template -FMT_CONSTEXPR14 void copy2(Char* dst, const char* src) { +template FMT_CONSTEXPR void copy2(Char* dst, const char* src) { *dst++ = static_cast(*src++); *dst = static_cast(*src); } FMT_INLINE FMT_CONSTEXPR20 void copy2(char* dst, const char* src) { - if (FMT_IS_CONSTANT_EVALUATED) { + if (is_constant_evaluated()) { copy2(dst, src); return; } @@ -1131,7 +1130,7 @@ inline FMT_CONSTEXPR20 format_decimal_result format_decimal(Char* out, int size) { FMT_ASSERT(size >= count_digits(value), "invalid digit count"); const data_digit_pair* digits; - if (FMT_IS_CONSTANT_EVALUATED) { + if (is_constant_evaluated()) { digits = compile_time_formatting_data::digits; } else { digits = data::digits; @@ -2115,7 +2114,7 @@ OutputIt write(OutputIt out, string_view value) { } template -FMT_CONSTEXPR14 OutputIt write(OutputIt out, basic_string_view value) { +FMT_CONSTEXPR OutputIt write(OutputIt out, basic_string_view value) { auto it = reserve(out, value.size()); it = copy_str(value.begin(), value.end(), it); return base_iterator(out, it); @@ -2132,7 +2131,7 @@ template ::value && !std::is_same::value && !std::is_same::value)> -FMT_CONSTEXPR14 OutputIt write(OutputIt out, T value) { +FMT_CONSTEXPR OutputIt write(OutputIt out, T value) { auto abs_value = static_cast>(value); bool negative = is_negative(value); // Don't do -abs_value since it trips unsigned-integer-overflow sanitizer. @@ -2156,14 +2155,14 @@ constexpr OutputIt write(OutputIt out, bool value) { } template -FMT_CONSTEXPR14 OutputIt write(OutputIt out, Char value) { +FMT_CONSTEXPR OutputIt write(OutputIt out, Char value) { auto it = reserve(out, 1); *it++ = value; return base_iterator(out, it); } template -FMT_CONSTEXPR14 OutputIt write(OutputIt out, const Char* value) { +FMT_CONSTEXPR OutputIt write(OutputIt out, const Char* value) { if (!value) { FMT_THROW(format_error("string pointer is null")); } else {