diff --git a/include/fmt/base.h b/include/fmt/base.h index 7cd243f9..f91f8e82 100644 --- a/include/fmt/base.h +++ b/include/fmt/base.h @@ -332,6 +332,13 @@ struct monostate { # define FMT_ENABLE_IF(...) fmt::enable_if_t<(__VA_ARGS__), int> = 0 #endif +template constexpr auto min_of(T a, T b) -> T { + return a < b ? a : b; +} +template constexpr auto max_of(T a, T b) -> T { + return a > b ? a : b; +} + namespace detail { // Suppresses "unused variable" warnings with the method described in // https://herbsutter.com/2009/10/18/mailbag-shutting-up-compiler-warnings/. @@ -562,8 +569,8 @@ template class basic_string_view { // Lexicographically compare this string reference to other. FMT_CONSTEXPR auto compare(basic_string_view other) const -> int { - size_t str_size = size_ < other.size_ ? size_ : other.size_; - int result = detail::compare(data_, other.data_, str_size); + int result = + detail::compare(data_, other.data_, min_of(size_, other.size_)); if (result != 0) return result; return size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1); } @@ -1627,12 +1634,12 @@ template struct arg_pack {}; template class format_string_checker { private: - type types_[NUM_ARGS > 0 ? NUM_ARGS : 1]; - named_arg_info named_args_[NUM_NAMED_ARGS > 0 ? NUM_NAMED_ARGS : 1]; + type types_[max_of(1, NUM_ARGS)]; + named_arg_info named_args_[max_of(1, NUM_NAMED_ARGS)]; compile_parse_context context_; using parse_func = auto (*)(parse_context&) -> const Char*; - parse_func parse_funcs_[NUM_ARGS > 0 ? NUM_ARGS : 1]; + parse_func parse_funcs_[max_of(1, NUM_ARGS)]; public: template @@ -1740,7 +1747,7 @@ template class buffer { // the new elements may not be initialized. FMT_CONSTEXPR void try_resize(size_t count) { try_reserve(count); - size_ = count <= capacity_ ? count : capacity_; + size_ = min_of(count, capacity_); } // Tries increasing the buffer capacity to `new_capacity`. It can increase the @@ -1804,7 +1811,7 @@ class fixed_buffer_traits { FMT_CONSTEXPR auto limit(size_t size) -> size_t { size_t n = limit_ > count_ ? limit_ - count_ : 0; count_ += size; - return size < n ? size : n; + return min_of(size, n); } }; @@ -2255,8 +2262,7 @@ template struct named_arg_store { // args_[0].named_args points to named_args to avoid bloating format_args. - // +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning. - arg_t args[1 + (NUM_ARGS != 0 ? NUM_ARGS : +1)]; + arg_t args[1 + NUM_ARGS]; named_arg_info named_args[NUM_NAMED_ARGS]; template @@ -2290,7 +2296,7 @@ struct format_arg_store { // +1 to workaround a bug in gcc 7.5 that causes duplicated-branches warning. using type = conditional_t[NUM_ARGS != 0 ? NUM_ARGS : +1], + arg_t[max_of(1, NUM_ARGS)], named_arg_store>; type args; }; diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index caf8e41a..cc08ff81 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1071,7 +1071,7 @@ void write_fractional_seconds(OutputIt& out, Duration d, int precision = -1) { } } else if (precision > 0) { *out++ = '.'; - leading_zeroes = (std::min)(leading_zeroes, precision); + leading_zeroes = min_of(leading_zeroes, precision); int remaining = precision - leading_zeroes; out = detail::fill_n(out, leading_zeroes, '0'); if (remaining < num_digits) { diff --git a/include/fmt/format.h b/include/fmt/format.h index 88421c70..c8c35f62 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2680,9 +2680,6 @@ class bigint { return bigits_[to_unsigned(index)]; } - static constexpr auto minimum(int a, int b) -> int { return a < b ? a : b; } - static constexpr auto maximum(int a, int b) -> int { return a > b ? a : b; } - FMT_CONSTEXPR auto get_bigit(int i) const -> bigit { return i >= exp_ && i < num_bigits() ? bigits_[i - exp_] : 0; }; @@ -2820,12 +2817,12 @@ class bigint { // Returns compare(lhs1 + lhs2, rhs). friend FMT_CONSTEXPR auto add_compare(const bigint& lhs1, const bigint& lhs2, const bigint& rhs) -> int { - int max_lhs_bigits = maximum(lhs1.num_bigits(), lhs2.num_bigits()); + int max_lhs_bigits = max_of(lhs1.num_bigits(), lhs2.num_bigits()); int num_rhs_bigits = rhs.num_bigits(); if (max_lhs_bigits + 1 < num_rhs_bigits) return -1; if (max_lhs_bigits > num_rhs_bigits) return 1; double_bigit borrow = 0; - int min_exp = minimum(minimum(lhs1.exp_, lhs2.exp_), rhs.exp_); + int min_exp = min_of(min_of(lhs1.exp_, lhs2.exp_), rhs.exp_); for (int i = num_rhs_bigits - 1; i >= min_exp; --i) { double_bigit sum = static_cast(lhs1.get_bigit(i)) + lhs2.get_bigit(i);