https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109772

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> std::chrono::hh_mm_ss hms(std::chrono::duration<int, std::ratio<1, 
> 1024>>{1511});

The bug affects this example because __fits<uint_least32_t> is true, because
duration_values<int>::max() <= duration_values<uint_least32_t>::max() is true.
So the library decides it can use uint_least32_t to store the subseconds part.
However, the value we need to store is duration<int64_t, ratio<1,
10'000'000'000>>(4755859375) not duration<int, ratio<1, 1024>(1511 % 1024) and
so we truncate 9990234375 to 32 bits.

The bug doesn't affect uses of hh_mm_ss with the standard duration aliases in
<chrono>, e.g. hh_mm_ss<nanoseconds>, because the standard chrono aliases with
higher precision than one second all use 64-bit rep types, so
__fits<uint_least32_t> is false. And in practice the number of users of
hh_mm_ss with durations with unusual periods and 32-bit rep types is probably
tiny. And hh_mm_ss should not be used for storage, only as a short-lived stack
variable ... so I think we can probably just fix this for 13.2 even though
that's theoretically an ABI break (the C++20 ABI isn't final yet, but we
usually try to keep it stable between minor releases like 13.1 and 13.2). We
can consider adding an abi_tag attribute to hh_mm_ss for 13.2 (but not on
trunk).

Reply via email to