This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++11 status.

934. duration is missing operator%

Section: 29.5 [time.duration] Status: C++11 Submitter: Terry Golubiewski Opened: 2008-11-30 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [time.duration].

View all issues with C++11 status.

Discussion:

Addresses US 81

duration is missing operator%. This operator is convenient for computing where in a time frame a given duration lies. A motivating example is converting a duration into a "broken-down" time duration such as hours::minutes::seconds:

class ClockTime
{
    typedef std::chrono::hours hours;
    typedef std::chrono::minutes minutes;
    typedef std::chrono::seconds seconds;
public:
    hours hours_;
    minutes minutes_;
    seconds seconds_;

    template <class Rep, class Period>
      explicit ClockTime(const std::chrono::duration<Rep, Period>& d)
        : hours_  (std::chrono::duration_cast<hours>  (d)),
          minutes_(std::chrono::duration_cast<minutes>(d % hours(1))),
          seconds_(std::chrono::duration_cast<seconds>(d % minutes(1)))
          {}
};

[ Summit: ]

Agree except that there is a typo in the proposed resolution. The member operators should be operator%=.

[ Batavia (2009-05): ]

We agree with the proposed resolution. Move to Tentatively Ready.

[ 2009-07 Frankfurt ]

Moved from Tentatively Ready to Open only because the wording needs to be improved for enable_if type constraining, possibly following Robert's formula.

[ 2009-07 Frankfurt: ]

Howard to open a separate issue (1177) to handle the removal of member functions from overload sets, provide wording, and possibly demonstrate how this can be implemented using enable_if (see 947).

Move to Ready.

Proposed resolution:

Add to the synopsis in 29 [time]:

template <class Rep1, class Period, class Rep2>
  duration<typename common_type<Rep1, Rep2>::type, Period>
  operator%(const duration<Rep1, Period>& d, const Rep2& s);
template <class Rep1, class Period1, class Rep2, class Period2>
  typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
  operator%(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

Add to the synopsis of duration in 29.5 [time.duration]:

template <class Rep, class Period = ratio<1>>
class duration {
public:
  ...
  duration& operator%=(const rep& rhs);
  duration& operator%=(const duration& d);
  ...
};

Add to 29.5.4 [time.duration.arithmetic]:

duration& operator%=(const rep& rhs);

Effects: rep_ %= rhs.

Returns: *this.

duration& operator%=(const duration& d);

Effects: rep_ %= d.count().

Returns: *this.

Add to 29.5.6 [time.duration.nonmember]:

template <class Rep1, class Period, class Rep2>
  duration<typename common_type<Rep1, Rep2>::type, Period>
  operator%(const duration<Rep1, Period>& d, const Rep2& s);

Requires: Rep2 shall be implicitly convertible to CR(Rep1, Rep2) and Rep2 shall not be an instantiation of duration. Diagnostic required.

Returns: duration<CR, Period>(d) %= s.

template <class Rep1, class Period1, class Rep2, class Period2>
  typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
  operator%(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

Returns: common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type(lhs) %= rhs.