This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of NAD status.
std::chrono::abs(duration)
is ill-formed with non-reduced periodsSection: 30.5.10 [time.duration.alg] Status: NAD Submitter: Charlie Barto Opened: 2022-07-16 Last modified: 2022-11-30
Priority: Not Prioritized
View all issues with NAD status.
Discussion:
Currently 30.5.10 [time.duration.alg] specifies abs(duration)
as:
Returns: if
d >= d.zero()
, returnd
, otherwise return-d
.
Because unary minus on durations is defined to return common_type_t<duration>(-rep_)
,
and common_type_t
for durations is specified to reduce the period, this is ill-formed
with durations such as duration<int, ratio<1000, 1000>>
, or any other
type where the numerator and denominator of the period are not coprime.
[2022-08-23; Reflector poll: NAD]
Not ill-formed, implementation should do a conversion. Changing it to return the reduced duration as an improvement would be for LEWG.
[2022-11-30 LWG telecon. Status changed: Tentatively NAD → NAD.]
Proposed resolution:
This wording is relative to N4910.
Modify 30.2 [time.syn], header <chrono>
synopsis, as indicated:
[…] // 30.5.10 [time.duration.alg], specialized algorithms template<class Rep, class Period> constexpr common_type_t<duration<Rep, Period>> abs(duration<Rep, Period> d); […]
Modify 30.5.10 [time.duration.alg] as indicated:
[Drafting note: This will cause
This is not a breaking change, because code that was usingabs
to reduce the period before returning it, much like the other arithmetic operators.abs
with a non-reduced period before did not compile. ]
template<class Rep, class Period> constexpr common_type_t<duration<Rep, Period>> abs(duration<Rep, Period> d);-1- Constraints:
-2- Returns: Ifnumeric_limits<Rep>::is_signed
istrue
.d >= d.zero()
, return+d
, otherwise return-d
.