From 5e184888990d68a563533461edffc94b3be8b934 Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Sat, 5 Sep 2020 16:33:44 -0400 Subject: [PATCH] Fix subtle bug in fractional_width --- include/date/date.h | 12 ++++++----- test/date_test/detail/width.pass.cpp | 30 ++++++++++++++-------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/include/date/date.h b/include/date/date.h index 00b2993..6c7787c 100644 --- a/include/date/date.h +++ b/include/date/date.h @@ -3685,11 +3685,12 @@ struct undocumented {explicit undocumented() = default;}; // Example: width<4>::value == 2 // Example: width<10>::value == 1 // Example: width<1000>::value == 3 -template +template struct width { - static CONSTDATA unsigned value = 1 + width::value; + static_assert(d > 0, "width called with zero denominator"); + static CONSTDATA unsigned value = 1 + width::value; }; template @@ -3718,9 +3719,10 @@ class decimal_format_seconds { using CT = typename std::common_type::type; using rep = typename CT::rep; + static unsigned CONSTDATA trial_width = + detail::width::value; public: - static unsigned CONSTDATA width = detail::width::value < 19 ? - detail::width::value : 6u; + static unsigned CONSTDATA width = trial_width < 19 ? trial_width : 6u; using precision = std::chrono::duration::value>>; diff --git a/test/date_test/detail/width.pass.cpp b/test/date_test/detail/width.pass.cpp index 6fbb7b9..94f57b7 100644 --- a/test/date_test/detail/width.pass.cpp +++ b/test/date_test/detail/width.pass.cpp @@ -46,19 +46,19 @@ int main() { using namespace date::detail; - static_assert(width<0>::value == 0, ""); - static_assert(width<1>::value == 0, ""); - static_assert(width<2>::value == 1, ""); - static_assert(width<3>::value == 19, ""); - static_assert(width<4>::value == 2, ""); - static_assert(width<5>::value == 1, ""); - static_assert(width<6>::value == 19, ""); - static_assert(width<7>::value == 19, ""); - static_assert(width<8>::value == 3, ""); - static_assert(width<9>::value == 19, ""); - static_assert(width<10>::value == 1, ""); - static_assert(width<100>::value == 2, ""); - static_assert(width<1000>::value == 3, ""); - static_assert(width<10000>::value == 4, ""); - static_assert(width<625>::value == 4, ""); + static_assert(width<0, 1>::value == 0, ""); + static_assert(width<1, 1>::value == 0, ""); + static_assert(width<1, 2>::value == 1, ""); + static_assert(width<1, 3>::value == 19, ""); + static_assert(width<1, 4>::value == 2, ""); + static_assert(width<1, 5>::value == 1, ""); + static_assert(width<1, 6>::value == 19, ""); + static_assert(width<1, 7>::value == 19, ""); + static_assert(width<1, 8>::value == 3, ""); + static_assert(width<1, 9>::value == 19, ""); + static_assert(width<1, 10>::value == 1, ""); + static_assert(width<1, 100>::value == 2, ""); + static_assert(width<1, 1000>::value == 3, ""); + static_assert(width<1, 10000>::value == 4, ""); + static_assert(width<756, 625>::value == 4, ""); }