Skip to content

Commit

Permalink
Update isfinite declaration to be constexpr for c++23 only
Browse files Browse the repository at this point in the history
Dependent function std::isfinite is constexpr for c++23.
https://en.cppreference.com/w/cpp/numeric/math/isfinite

Fix fmtlib#3745
  • Loading branch information
hchataing committed Dec 9, 2023
1 parent dee0dbf commit 04338ce
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@
# define FMT_CONSTEXPR20
#endif

#if (FMT_CPLUSPLUS >= 202302L)
# define FMT_CONSTEXPR23 constexpr
#else
# define FMT_CONSTEXPR23
#endif

// Check if constexpr std::char_traits<>::{compare,length} are supported.
#if defined(__GLIBCXX__)
# if FMT_CPLUSPLUS >= 201703L && defined(_GLIBCXX_RELEASE) && \
Expand Down
6 changes: 3 additions & 3 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2750,7 +2750,7 @@ struct has_isfinite<T, enable_if_t<sizeof(std::isfinite(T())) != 0>>

template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value&&
has_isfinite<T>::value)>
FMT_CONSTEXPR20 bool isfinite(T value) {
FMT_CONSTEXPR23 bool isfinite(T value) {
constexpr T inf = T(std::numeric_limits<double>::infinity());
if (is_constant_evaluated())
return !detail::isnan(value) && value < inf && value > -inf;
Expand Down Expand Up @@ -3587,7 +3587,7 @@ FMT_CONSTEXPR20 auto format_float(Float value, int precision, float_specs specs,
return exp;
}
template <typename Char, typename OutputIt, typename T>
FMT_CONSTEXPR20 auto write_float(OutputIt out, T value,
FMT_CONSTEXPR23 auto write_float(OutputIt out, T value,
format_specs<Char> specs, locale_ref loc)
-> OutputIt {
float_specs fspecs = parse_float_type_spec(specs);
Expand Down Expand Up @@ -3637,7 +3637,7 @@ FMT_CONSTEXPR20 auto write_float(OutputIt out, T value,

template <typename Char, typename OutputIt, typename T,
FMT_ENABLE_IF(is_floating_point<T>::value)>
FMT_CONSTEXPR20 auto write(OutputIt out, T value, format_specs<Char> specs,
FMT_CONSTEXPR23 auto write(OutputIt out, T value, format_specs<Char> specs,
locale_ref loc = {}) -> OutputIt {
if (const_check(!is_supported_floating_point(value))) return out;
return specs.localized && write_loc(out, value, specs, loc)
Expand Down
2 changes: 1 addition & 1 deletion test/compile-fp-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#if defined(__cpp_lib_bit_cast) && __cpp_lib_bit_cast >= 201806 && \
defined(__cpp_constexpr) && __cpp_constexpr >= 201907 && \
defined(__cpp_constexpr_dynamic_alloc) && \
__cpp_constexpr_dynamic_alloc >= 201907 && FMT_CPLUSPLUS >= 202002L
__cpp_constexpr_dynamic_alloc >= 201907 && FMT_CPLUSPLUS >= 202302L

template <size_t max_string_length, typename Char = char> struct test_string {
template <typename T> constexpr bool operator==(const T& rhs) const noexcept {
Expand Down
2 changes: 1 addition & 1 deletion test/compile-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ TEST(compile_test, constexpr_formatted_size) {
FMT_CONSTEXPR20 size_t padded_size =
fmt::formatted_size(FMT_COMPILE("{:*^6}"), 42);
EXPECT_EQ(padded_size, 6);
FMT_CONSTEXPR20 size_t float_size =
FMT_CONSTEXPR23 size_t float_size =
fmt::formatted_size(FMT_COMPILE("{:.3}"), 12.345);
EXPECT_EQ(float_size, 4);
FMT_CONSTEXPR20 size_t str_size =
Expand Down
2 changes: 1 addition & 1 deletion test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ template <typename Float> void check_isfinite() {
EXPECT_TRUE(isfinite(Float(fmt::detail::max_value<double>())));
// Use double because std::numeric_limits is broken for __float128.
using limits = std::numeric_limits<double>;
FMT_CONSTEXPR20 auto result = isfinite(Float(limits::infinity()));
FMT_CONSTEXPR23 auto result = isfinite(Float(limits::infinity()));
EXPECT_FALSE(result);
EXPECT_FALSE(isfinite(Float(limits::infinity())));
EXPECT_FALSE(isfinite(Float(-limits::infinity())));
Expand Down

0 comments on commit 04338ce

Please sign in to comment.