Title
duration conversion overflow shouldn't participate in overload resolution
Status
c++14
Section
[time.duration.cons]
Submitter
Vicente J. Botet Escriba

Created on 2011-10-31.00:00:00 last changed 123 months ago

Messages

Date: 2013-04-19.21:55:15

Proposed resolution:

This wording is relative to the FDIS.

Change the following paragraphs of [time.duration.cons] p4 indicated:

template <class Rep2, class Period2>
  constexpr duration(const duration<Rep2, Period2>& d);

Remarks: This constructor shall not participate in overload resolution unless no overflow is induced in the conversion and treat_as_floating_point<rep>::value is true or both ratio_divide<Period2, period>::den is 1 and treat_as_floating_point<Rep2>::value is false. [ Note: This requirement prevents implicit truncation error when converting between integral-based duration types. Such a construction could easily lead to confusion about the value of the duration. — end note ]

Date: 2013-04-15.00:00:00

[ 2013-04-18, Bristol ]

Date: 2012-02-27.20:05:44

[ 2012, Kona ]

Move to Review.

Pete: The wording is not right.

Howard: Will implement this to be sure it works.

Jeffrey: If ratio needs a new hook, should it be exposed to users for their own uses?

Pete: No.

Move to Review, Howard to implement in a way that mere mortals can understand.

Date: 2011-10-31.00:00:00

[time.duration.cons] says:

template <class Rep2, class Period2>
  constexpr duration(const duration<Rep2, Period2>& d);

Remarks: This constructor shall not participate in overload resolution unless treat_as_floating_point<rep>::value is true or both ratio_divide<Period2, period>::den is 1 and treat_as_floating_point<Rep2>::value is false.

The evaluation of ratio_divide<Period2, period>::den could make ratio_divide<Period2, period>::num overflow.

This occur for example when we try to create a millisecond (period=ratio<1,1000>) from an exa-second (Period2=ratio<1018>).

ratio_divide<ratio<1018>, ratio<1,1000>>::num is 1021 which overflows which makes the compiler error.

If the function f is overloaded with milliseconds and seconds

void f(milliseconds);
void f(seconds);

The following fails to compile.

duration<int,exa> r(1);
f(r);

While the conversion to seconds work, the conversion to milliseconds make the program fail at compile time. In my opinion, this program should be well formed and the constructor from duration<int,exa> to milliseconds shouldn't participate in overload resolution as the result can not be represented.

I think the wording of the standard can be improved so no misinterpretations are possible by adding that "no overflow is induced by the conversion".

History
Date User Action Args
2014-02-20 13:20:35adminsetstatus: wp -> c++14
2013-04-25 19:07:07adminsetstatus: voting -> wp
2013-04-19 21:55:15adminsetmessages: + msg6469
2013-04-19 21:44:50adminsetstatus: ready -> voting
2013-04-18 22:58:13adminsetstatus: review -> ready
2012-02-27 20:05:44adminsetstatus: ready -> review
2012-02-12 18:36:43adminsetmessages: + msg6002
2012-02-12 18:36:43adminsetstatus: new -> ready
2011-11-27 18:12:45adminsetmessages: + msg5918
2011-10-31 00:00:00admincreate