Title
std::chrono::abs(duration) is ill-formed with non-reduced periods
Status
nad
Section
[time.duration.alg]
Submitter
Charlie Barto

Created on 2022-07-16.00:00:00 last changed 25 months ago

Messages

Date: 2022-11-30.17:59:24

Proposed resolution:

This wording is relative to N4910.

  1. Modify [time.syn], header <chrono> synopsis, as indicated:

    […]
    // [time.duration.alg], specialized algorithms
    template<class Rep, class Period>
      constexpr common_type_t<duration<Rep, Period>> abs(duration<Rep, Period> d);
    […]
    
  2. Modify [time.duration.alg] as indicated:

    [Drafting note: This will cause abs to reduce the period before returning it, much like the other arithmetic operators.

    This is not a breaking change, because code that was using 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: numeric_limits<Rep>::is_signed is true.

    -2- Returns: If d >= d.zero(), return +d, otherwise return -d.

Date: 2022-11-30.00:00:00

[ 2022-11-30 LWG telecon. Status changed: Tentatively NAD → NAD. ]

Date: 2022-08-15.00:00:00

[ 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.

Date: 2022-07-16.00:00:00

Currently [time.duration.alg] specifies abs(duration) as:

Returns: if d >= d.zero(), return d, 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.

History
Date User Action Args
2022-11-30 17:59:24adminsetmessages: + msg13138
2022-08-23 15:00:26adminsetmessages: + msg12688
2022-08-23 15:00:26adminsetstatus: new -> nad
2022-07-16 18:32:16adminsetmessages: + msg12598
2022-07-16 00:00:00admincreate