From c8deb52afd5caf9912c8150af420396713ac0eff Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Mon, 21 Aug 2023 21:15:36 -0700 Subject: [PATCH] floating_point_integral_constant Summary: Like `std::integral_constant`, but for holding integral values of floating-point type representable in an integral type. Reviewed By: ispeters Differential Revision: D48483298 fbshipit-source-id: 68e712da695e38503b3a89d2eeb73dad515d0e1f --- folly/ConstexprMath.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/folly/ConstexprMath.h b/folly/ConstexprMath.h index 1e5a2ceb8fe..39f83c4f130 100644 --- a/folly/ConstexprMath.h +++ b/folly/ConstexprMath.h @@ -62,6 +62,23 @@ FOLLY_INLINE_VARIABLE constexpr double ln2 = ln2_v; } // namespace numbers +/// floating_point_integral_constant +/// +/// Like std::integral_constant but for floating-point types holding integral +/// values representable in an integral type. +template +struct floating_point_integral_constant { + using value_type = T; + static constexpr value_type value = static_cast(Value); + constexpr operator value_type() const noexcept { return value; } + constexpr value_type operator()() const noexcept { return value; } +}; +#if FOLLY_CPLUSPLUS < 201703L +template +constexpr typename floating_point_integral_constant::value_type + floating_point_integral_constant::value; +#endif + // TLDR: Prefer using operator< for ordering. And when // a and b are equivalent objects, we return b to make // sorting stable.