From 45f230b9b2dc78f2efd3b9a3baff518a2d717c60 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 29 Jan 2026 17:29:33 -0800 Subject: [PATCH] Remove const_check --- include/fmt/args.h | 4 ++-- include/fmt/chrono.h | 31 ++++++++++++++------------- include/fmt/format.h | 45 ++++++++++++++++++---------------------- include/fmt/ostream.h | 2 +- include/fmt/printf.h | 2 +- include/fmt/ranges.h | 12 +++++------ test/format-impl-test.cc | 4 ++-- test/format-test.cc | 8 +++---- test/printf-test.cc | 5 ++--- test/xchar-test.cc | 2 +- 10 files changed, 54 insertions(+), 61 deletions(-) diff --git a/include/fmt/args.h b/include/fmt/args.h index 5e5f40f9..6b8c0a62 100644 --- a/include/fmt/args.h +++ b/include/fmt/args.h @@ -152,7 +152,7 @@ FMT_EXPORT template class dynamic_format_arg_store { * std::string result = fmt::vformat("{} and {} and {}", store); */ template void push_back(const T& arg) { - if (detail::const_check(need_copy::value)) + if FMT_CONSTEXPR20 (need_copy::value) emplace_arg(dynamic_args_.push>(arg)); else emplace_arg(detail::unwrap(arg)); @@ -187,7 +187,7 @@ FMT_EXPORT template class dynamic_format_arg_store { void push_back(const detail::named_arg& arg) { const char_type* arg_name = dynamic_args_.push>(arg.name).c_str(); - if (detail::const_check(need_copy::value)) { + if FMT_CONSTEXPR20 (need_copy::value) { emplace_arg( fmt::arg(arg_name, dynamic_args_.push>(arg.value))); } else { diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index fe86ef8d..752b9699 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -52,7 +52,7 @@ FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) static_assert(T::is_integer, "To must be integral"); // A and B are both signed, or both unsigned. - if (detail::const_check(F::digits <= T::digits)) { + if FMT_CONSTEXPR20 (F::digits <= T::digits) { // From fits in To without any problem. } else { // From does not always fit in To, resort to a dynamic check. @@ -79,22 +79,21 @@ FMT_CONSTEXPR auto lossless_integral_conversion(const From from, int& ec) static_assert(F::is_integer, "From must be integral"); static_assert(T::is_integer, "To must be integral"); - if (detail::const_check(F::is_signed && !T::is_signed)) { + if FMT_CONSTEXPR20 (F::is_signed && !T::is_signed) { // From may be negative, not allowed! if (fmt::detail::is_negative(from)) { ec = 1; return {}; } // From is positive. Can it always fit in To? - if (detail::const_check(F::digits > T::digits) && + if (F::digits > T::digits && from > static_cast(detail::max_value())) { ec = 1; return {}; } } - if (detail::const_check(!F::is_signed && T::is_signed && - F::digits >= T::digits) && + if (!F::is_signed && T::is_signed && F::digits >= T::digits && from > static_cast(detail::max_value())) { ec = 1; return {}; @@ -188,7 +187,7 @@ auto safe_duration_cast(std::chrono::duration from, } // multiply with Factor::num without overflow or underflow - if (detail::const_check(Factor::num != 1)) { + if FMT_CONSTEXPR20 (Factor::num != 1) { constexpr auto max1 = detail::max_value() / static_cast(Factor::num); if (count > max1) { @@ -205,7 +204,7 @@ auto safe_duration_cast(std::chrono::duration from, } // this can't go wrong, right? den>0 is checked earlier. - if (detail::const_check(Factor::den != 1)) { + if FMT_CONSTEXPR20 (Factor::den != 1) { using common_t = typename std::common_type::type; count /= static_cast(Factor::den); } @@ -337,7 +336,7 @@ void write_codecvt(codecvt_result& out, string_view in, template auto write_encoded_tm_str(OutputIt out, string_view in, const std::locale& loc) -> OutputIt { - if (const_check(detail::use_utf8) && loc != get_classic_locale()) { + if (detail::use_utf8 && loc != get_classic_locale()) { // char16_t and char32_t codecvts are broken in MSVC (linkage errors) and // gcc-4. #if FMT_MSC_VERSION != 0 || \ @@ -434,14 +433,14 @@ auto duration_cast(std::chrono::duration from) -> To { common_rep count = from.count(); // This conversion is lossless. // Multiply from.count() by factor and check for overflow. - if (const_check(factor::num != 1)) { + if FMT_CONSTEXPR20 (factor::num != 1) { if (count > max_value() / factor::num) throw_duration_error(); const auto min = (std::numeric_limits::min)() / factor::num; - if (const_check(!std::is_unsigned::value) && count < min) + if (!std::is_unsigned::value && count < min) throw_duration_error(); count *= factor::num; } - if (const_check(factor::den != 1)) count /= factor::den; + if FMT_CONSTEXPR20 (factor::den != 1) count /= factor::den; int ec = 0; auto to = To(safe_duration_cast::lossless_integral_conversion( @@ -563,7 +562,7 @@ inline void write_digit2_separated(char* buf, unsigned a, unsigned b, digits |= 0x3030003030003030 | (usep << 16) | (usep << 40); constexpr size_t len = 8; - if (const_check(is_big_endian())) { + if (is_big_endian()) { char tmp[len]; std::memcpy(tmp, &digits, len); std::reverse_copy(tmp, tmp + len, buf); @@ -1575,7 +1574,7 @@ auto format_duration_unit(OutputIt out) -> OutputIt { return copy_unit(string_view(unit), out, Char()); *out++ = '['; out = write(out, Period::num); - if (const_check(Period::den != 1)) { + if FMT_CONSTEXPR20 (Period::den != 1) { *out++ = '/'; out = write(out, Period::den); } @@ -2178,9 +2177,9 @@ struct formatter, Char> : private formatter { -> decltype(ctx.out()) { std::tm tm = gmtime(val); using period = typename Duration::period; - if (detail::const_check( - period::num == 1 && period::den == 1 && - !std::is_floating_point::value)) { + if FMT_CONSTEXPR20 (period::num == 1 && period::den == 1 && + !std::is_floating_point< + typename Duration::rep>::value) { detail::set_tm_zone(tm, detail::utc()); return formatter::format(tm, ctx); } diff --git a/include/fmt/format.h b/include/fmt/format.h index 0e2339d1..94e964b4 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -248,11 +248,6 @@ inline auto clzll(uint64_t x) -> int { # define FMT_BUILTIN_CLZLL(n) detail::clzll(n) #endif // FMT_MSC_VERSION && !defined(FMT_BUILTIN_CLZLL) -// Suppresses "conditional expression is constant" warnings. -template FMT_ALWAYS_INLINE constexpr auto const_check(T val) -> T { - return val; -} - FMT_CONSTEXPR inline void abort_fuzzing_if(bool condition) { ignore_unused(condition); #ifdef FMT_FUZZ @@ -448,7 +443,7 @@ inline auto bit_cast(const From& from) -> To { unsigned short value[static_cast(size)]; } data = bit_cast(from); auto result = To(); - if (const_check(is_big_endian())) { + if (is_big_endian()) { for (int i = 0; i < size; ++i) result = (result << num_bits()) | data.value[i]; } else { @@ -1495,26 +1490,26 @@ template struct float_info; template <> struct float_info { using carrier_uint = uint32_t; - static const int exponent_bits = 8; - static const int kappa = 1; - static const int big_divisor = 100; - static const int small_divisor = 10; - static const int min_k = -31; - static const int max_k = 46; - static const int shorter_interval_tie_lower_threshold = -35; - static const int shorter_interval_tie_upper_threshold = -35; + static constexpr int exponent_bits = 8; + static constexpr int kappa = 1; + static constexpr int big_divisor = 100; + static constexpr int small_divisor = 10; + static constexpr int min_k = -31; + enum { max_k = 46 }; + static constexpr int shorter_interval_tie_lower_threshold = -35; + static constexpr int shorter_interval_tie_upper_threshold = -35; }; template <> struct float_info { using carrier_uint = uint64_t; - static const int exponent_bits = 11; - static const int kappa = 2; - static const int big_divisor = 1000; - static const int small_divisor = 100; - static const int min_k = -292; - static const int max_k = 341; - static const int shorter_interval_tie_lower_threshold = -77; - static const int shorter_interval_tie_upper_threshold = -77; + static constexpr int exponent_bits = 11; + static constexpr int kappa = 2; + static constexpr int big_divisor = 1000; + static constexpr int small_divisor = 100; + static constexpr int min_k = -292; + enum { max_k = 341 }; + static constexpr int shorter_interval_tie_lower_threshold = -77; + static constexpr int shorter_interval_tie_upper_threshold = -77; }; // An 80- or 128-bit floating point number. @@ -1781,7 +1776,7 @@ FMT_API auto is_printable(uint32_t cp) -> bool; inline auto needs_escape(uint32_t cp) -> bool { if (cp < 0x20 || cp == 0x7f || cp == '"' || cp == '\\') return true; - if (const_check(FMT_OPTIMIZE_SIZE > 1)) return false; + if FMT_CONSTEXPR20 (FMT_OPTIMIZE_SIZE > 1) return false; return !is_printable(cp); } @@ -1796,7 +1791,7 @@ auto find_escape(const Char* begin, const Char* end) -> find_escape_result { for (; begin != end; ++begin) { uint32_t cp = static_cast>(*begin); - if (const_check(sizeof(Char) == 1) && cp >= 0x80) continue; + if (sizeof(Char) == 1 && cp >= 0x80) continue; if (needs_escape(cp)) return {begin, begin + 1, cp}; } return {begin, nullptr, 0}; @@ -1804,7 +1799,7 @@ auto find_escape(const Char* begin, const Char* end) inline auto find_escape(const char* begin, const char* end) -> find_escape_result { - if (const_check(!use_utf8)) return find_escape(begin, end); + if FMT_CONSTEXPR20 (!use_utf8) return find_escape(begin, end); auto result = find_escape_result{end, nullptr, 0}; for_each_codepoint(string_view(begin, to_unsigned(end - begin)), [&](uint32_t cp, string_view sv) { diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 7bcc7cbb..a139e825 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -150,7 +150,7 @@ inline void vprint(std::ostream& os, string_view fmt, format_args args) { FMT_EXPORT template void print(std::ostream& os, format_string fmt, T&&... args) { fmt::vargs vargs = {{args...}}; - if (detail::const_check(detail::use_utf8)) return vprint(os, fmt.str, vargs); + if FMT_CONSTEXPR20 (detail::use_utf8) return vprint(os, fmt.str, vargs); auto buffer = memory_buffer(); detail::vformat_to(buffer, fmt.str, vargs); detail::write_buffer(os, buffer); diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 5285d79c..e4fa4967 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -136,7 +136,7 @@ template class arg_converter { void operator()(U value) { bool is_signed = type_ == 'd' || type_ == 'i'; using target_type = conditional_t::value, U, T>; - if (const_check(sizeof(target_type) <= sizeof(int))) { + if FMT_CONSTEXPR20 (sizeof(target_type) <= sizeof(int)) { // Extra casts are used to silence warnings. using unsigned_type = typename make_unsigned_or_bool::type; if (is_signed) diff --git a/include/fmt/ranges.h b/include/fmt/ranges.h index 36b38e29..89cdbc80 100644 --- a/include/fmt/ranges.h +++ b/include/fmt/ranges.h @@ -505,8 +505,7 @@ struct formatter< using nonlocking = void; FMT_CONSTEXPR formatter() { - if (detail::const_check(range_format_kind::value != - range_format::set)) + if FMT_CONSTEXPR20 (range_format_kind::value != range_format::set) return; range_formatter_.set_brackets(detail::string_literal{}, detail::string_literal{}); @@ -607,13 +606,14 @@ struct formatter< auto format(range_type& range, FormatContext& ctx) const -> decltype(ctx.out()) { auto out = ctx.out(); - if (detail::const_check(range_format_kind::value == - range_format::debug_string)) + if FMT_CONSTEXPR20 (range_format_kind::value == + range_format::debug_string) { *out++ = '"'; + } out = underlying_.format( string_type{detail::range_begin(range), detail::range_end(range)}, ctx); - if (detail::const_check(range_format_kind::value == - range_format::debug_string)) + if FMT_CONSTEXPR20 (range_format_kind::value == + range_format::debug_string) *out++ = '"'; return out; } diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 3b0e07e3..0d906c4e 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -200,12 +200,12 @@ TEST(fp_test, dragonbox_max_k) { using fmt::detail::dragonbox::floor_log10_pow2; using float_info = fmt::detail::dragonbox::float_info; EXPECT_EQ( - fmt::detail::const_check(float_info::max_k), + float_info::max_k, float_info::kappa - floor_log10_pow2(std::numeric_limits::min_exponent - fmt::detail::num_significand_bits() - 1)); using double_info = fmt::detail::dragonbox::float_info; - EXPECT_EQ(fmt::detail::const_check(double_info::max_k), + EXPECT_EQ(double_info::max_k, double_info::kappa - floor_log10_pow2( std::numeric_limits::min_exponent - diff --git a/test/format-test.cc b/test/format-test.cc index b7496085..4db939a1 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -995,7 +995,7 @@ TEST(format_test, runtime_width) { format_error, bad_dynamic_spec_msg); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, -1l), format_error, bad_dynamic_spec_msg); - if (fmt::detail::const_check(sizeof(long) > sizeof(int))) { + if (sizeof(long) > sizeof(int)) { long value = INT_MAX; EXPECT_THROW_MSG((void)fmt::format(runtime("{0:{1}}"), 0, (value + 1)), format_error, bad_dynamic_spec_msg); @@ -1243,7 +1243,7 @@ TEST(format_test, runtime_precision) { format_error, bad_dynamic_spec_msg); EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, -1l), format_error, bad_dynamic_spec_msg); - if (fmt::detail::const_check(sizeof(long) > sizeof(int))) { + if (sizeof(long) > sizeof(int)) { long value = INT_MAX; EXPECT_THROW_MSG((void)fmt::format(runtime("{0:.{1}}"), 0.0, (value + 1)), format_error, bad_dynamic_spec_msg); @@ -1519,7 +1519,7 @@ TEST(format_test, format_double) { } #endif - if (fmt::detail::const_check(std::numeric_limits::is_iec559)) { + if (std::numeric_limits::is_iec559) { double d = (std::numeric_limits::min)(); EXPECT_EQ(fmt::format("{:a}", d), "0x1p-1022"); EXPECT_EQ(fmt::format("{:#a}", d), "0x1.p-1022"); @@ -2489,7 +2489,7 @@ auto format_as(const string& s) -> std::string { return s; } TEST(format_test, adl) { // Only check compilation and don't run the code to avoid polluting the output // and since the output is tested elsewhere. - if (fmt::detail::const_check(true)) return; + if (true) return; auto s = adl_test::string(); char buf[10]; (void)fmt::format("{}", s); diff --git a/test/printf-test.cc b/test/printf-test.cc index 63035006..f232263c 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -348,11 +348,10 @@ void test_length(const char* length_spec, U value) { unsigned long long unsigned_value = 0; // Apply integer promotion to the argument. unsigned long long max = max_value(); - using fmt::detail::const_check; - if (const_check(max <= static_cast(max_value()))) { + if (max <= static_cast(max_value())) { signed_value = static_cast(value); unsigned_value = static_cast(value); - } else if (const_check(max <= max_value())) { + } else if (max <= max_value()) { signed_value = static_cast(value); unsigned_value = static_cast(value); } diff --git a/test/xchar-test.cc b/test/xchar-test.cc index b0646da0..42f5f178 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -159,7 +159,7 @@ TEST(xchar_test, named_arg_udl) { TEST(xchar_test, print) { // Check that the wide print overload compiles. - if (fmt::detail::const_check(false)) { + if (false) { fmt::print(L"test"); fmt::println(L"test"); }