Remove const_check

This commit is contained in:
Victor Zverovich 2026-01-29 17:29:33 -08:00
parent 99803085d4
commit 45f230b9b2
10 changed files with 54 additions and 61 deletions

View File

@ -152,7 +152,7 @@ FMT_EXPORT template <typename Context> class dynamic_format_arg_store {
* std::string result = fmt::vformat("{} and {} and {}", store);
*/
template <typename T> void push_back(const T& arg) {
if (detail::const_check(need_copy<T>::value))
if FMT_CONSTEXPR20 (need_copy<T>::value)
emplace_arg(dynamic_args_.push<stored_t<T>>(arg));
else
emplace_arg(detail::unwrap(arg));
@ -187,7 +187,7 @@ FMT_EXPORT template <typename Context> class dynamic_format_arg_store {
void push_back(const detail::named_arg<char_type, T>& arg) {
const char_type* arg_name =
dynamic_args_.push<std::basic_string<char_type>>(arg.name).c_str();
if (detail::const_check(need_copy<T>::value)) {
if FMT_CONSTEXPR20 (need_copy<T>::value) {
emplace_arg(
fmt::arg(arg_name, dynamic_args_.push<stored_t<T>>(arg.value)));
} else {

View File

@ -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<From>(detail::max_value<To>())) {
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<From>(detail::max_value<To>())) {
ec = 1;
return {};
@ -188,7 +187,7 @@ auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> 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<IntermediateRep>() /
static_cast<IntermediateRep>(Factor::num);
if (count > max1) {
@ -205,7 +204,7 @@ auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> 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<IntermediateRep, intmax_t>::type;
count /= static_cast<common_t>(Factor::den);
}
@ -337,7 +336,7 @@ void write_codecvt(codecvt_result<CodeUnit>& out, string_view in,
template <typename OutputIt>
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<FromRep, FromPeriod> 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<common_rep>() / factor::num) throw_duration_error();
const auto min = (std::numeric_limits<common_rep>::min)() / factor::num;
if (const_check(!std::is_unsigned<common_rep>::value) && count < min)
if (!std::is_unsigned<common_rep>::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<typename To::rep>(
@ -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<Char>(out, Period::num);
if (const_check(Period::den != 1)) {
if FMT_CONSTEXPR20 (Period::den != 1) {
*out++ = '/';
out = write<Char>(out, Period::den);
}
@ -2178,9 +2177,9 @@ struct formatter<sys_time<Duration>, Char> : private formatter<std::tm, Char> {
-> 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<typename Duration::rep>::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<std::tm, Char>::format(tm, ctx);
}

View File

@ -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 <typename T> 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<unsigned>(size)];
} data = bit_cast<data_t>(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<unsigned short>()) | data.value[i];
} else {
@ -1495,26 +1490,26 @@ template <typename T, typename Enable = void> struct float_info;
template <> struct float_info<float> {
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<double> {
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<Char> {
for (; begin != end; ++begin) {
uint32_t cp = static_cast<unsigned_char<Char>>(*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<char> {
if (const_check(!use_utf8)) return find_escape<char>(begin, end);
if FMT_CONSTEXPR20 (!use_utf8) return find_escape<char>(begin, end);
auto result = find_escape_result<char>{end, nullptr, 0};
for_each_codepoint(string_view(begin, to_unsigned(end - begin)),
[&](uint32_t cp, string_view sv) {

View File

@ -150,7 +150,7 @@ inline void vprint(std::ostream& os, string_view fmt, format_args args) {
FMT_EXPORT template <typename... T>
void print(std::ostream& os, format_string<T...> fmt, T&&... args) {
fmt::vargs<T...> 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);

View File

@ -136,7 +136,7 @@ template <typename T, typename Context> class arg_converter {
void operator()(U value) {
bool is_signed = type_ == 'd' || type_ == 'i';
using target_type = conditional_t<std::is_same<T, void>::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<target_type>::type;
if (is_signed)

View File

@ -505,8 +505,7 @@ struct formatter<
using nonlocking = void;
FMT_CONSTEXPR formatter() {
if (detail::const_check(range_format_kind<R, Char>::value !=
range_format::set))
if FMT_CONSTEXPR20 (range_format_kind<R, Char>::value != range_format::set)
return;
range_formatter_.set_brackets(detail::string_literal<Char, '{'>{},
detail::string_literal<Char, '}'>{});
@ -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<R, Char>::value ==
range_format::debug_string))
if FMT_CONSTEXPR20 (range_format_kind<R, Char>::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<R, Char>::value ==
range_format::debug_string))
if FMT_CONSTEXPR20 (range_format_kind<R, Char>::value ==
range_format::debug_string)
*out++ = '"';
return out;
}

View File

@ -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<float>;
EXPECT_EQ(
fmt::detail::const_check(float_info::max_k),
float_info::max_k,
float_info::kappa -
floor_log10_pow2(std::numeric_limits<float>::min_exponent -
fmt::detail::num_significand_bits<float>() - 1));
using double_info = fmt::detail::dragonbox::float_info<double>;
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<double>::min_exponent -

View File

@ -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<double>::is_iec559)) {
if (std::numeric_limits<double>::is_iec559) {
double d = (std::numeric_limits<double>::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);

View File

@ -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<U>();
using fmt::detail::const_check;
if (const_check(max <= static_cast<unsigned>(max_value<int>()))) {
if (max <= static_cast<unsigned>(max_value<int>())) {
signed_value = static_cast<int>(value);
unsigned_value = static_cast<unsigned long long>(value);
} else if (const_check(max <= max_value<unsigned>())) {
} else if (max <= max_value<unsigned>()) {
signed_value = static_cast<unsigned>(value);
unsigned_value = static_cast<unsigned long long>(value);
}

View File

@ -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");
}