1171. duration types should be literal

Section: 30.5 [time.duration] Status: C++11 Submitter: Alisdair Meredith Opened: 2009-07-06 Last modified: 2016-01-28

Priority: Not Prioritized

View all other issues in [time.duration].

View all issues with C++11 status.

Discussion:

The duration types in 30.5 [time.duration] are exactly the sort of type that should be "literal types" in the new standard. Likewise, arithmetic operations on durations should be declared constexpr.

[ 2009-09-21 Daniel adds: ]

An alternative (and possibly preferable solution for potentially heap-allocating big_int representation types) would be to ask the core language to allow references to const literal types as feasible arguments for constexpr functions.

[ 2009-10-30 Alisdair adds: ]

I suggest this issue moves from New to Open.

Half of this issue was dealt with in paper n2994 on constexpr constructors.

The other half (duration arithmetic) is on hold pending Core support for const & in constexpr functions.

[ 2010-03-15 Alisdair updated wording to be consistent with N3078. ]

[ 2010 Rapperswil: ]

This issue was the motivation for Core adding the facility for constexpr functions to take parameters by const &. Move to Tentatively Ready.

[ Adopted at 2010-11 Batavia. ]

Proposed resolution:

Add constexpr to declaration of following functions and constructors:

Modify p1 30 [time], and the prototype definitions in 30.5.6 [time.duration.nonmember], 30.5.7 [time.duration.comparisons], and 30.5.8 [time.duration.cast]:

Header <chrono> synopsis

// duration arithmetic
template <class Rep1, class Period1, class Rep2, class Period2>
   typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
   constexpr operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
   typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
   constexpr operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period, class Rep2>
   duration<typename common_type<Rep1, Rep2>::type, Period>
   constexpr operator*(const duration<Rep1, Period>& d, const Rep2& s);
template <class Rep1, class Period, class Rep2>
   duration<typename common_type<Rep1, Rep2>::type, Period>
   constexpr operator*(const Rep1& s, const duration<Rep2, Period>& d);
template <class Rep1, class Period, class Rep2>
   duration<typename common_type<Rep1, Rep2>::type, Period>
   constexpr operator/(const duration<Rep1, Period>& d, const Rep2& s);
template <class Rep1, class Period1, class Rep2, class Period2>
   typename common_type<Rep1, Rep2>::type
   constexpr operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

// duration comparisons
template <class Rep1, class Period1, class Rep2, class Period2>
   constexpr bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
   constexpr bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
   constexpr bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
   constexpr bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
   constexpr bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
   constexpr bool operator>=(const  duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);

// duration_cast
template <class ToDuration, class Rep, class Period>
   constexpr ToDuration duration_cast(const duration<Rep, Period>& d);

Change 30.5 [time.duration]:

template <class Rep, class Period = ratio<1>>
class duration {
  ...
public:
  ...
  constexpr duration(const duration&) = default;
  ...

};

[ Note - this edit already seems assumed by definition of the duration static members zero/min/max. They cannot meaningfully be constexpr without this change. ]