libstdc++: Constrain chrono::duration conversions [LWG 2094]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 1 Sep 2020 17:18:26 +0000 (18:18 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 1 Sep 2020 17:18:26 +0000 (18:18 +0100)
commitb1850c617b14eedaf60b358f3b7d4707cff73b8a
tree8c960987dcbd6e7fe0f0b3e060ec9bb3f1643de7
parent49bfbf18c0bb9d83934f0ce765dc031ebfbda38e
libstdc++: Constrain chrono::duration conversions [LWG 2094]

The chrono::duration constructor that converts from another duration
type is meant to be constrained so that it doesn't participate in
overload resolution if the ratio of the periods cannot be represented as
a std::ratio.

Because our std::ratio_divide is not SFINAE-friendly the evaluation of
__is_harmonic results in an error outside the immediate context when an
overflow occurs. I intend to make ratio_divide (and ratio_multiply)
SFINAE-friendly in a future patch, but for now this patch just
introduces a new SFINAE-friendly alias template for the division.

The standard doesn't require it, but it also seems right to constrain
the constructor with std::is_convertible_v<_Rep2, rep>.

libstdc++-v3/ChangeLog:

* include/std/chrono (duration::_S_gcd(intmax_t, intmax_t)):
New helper function for finding GCD of two positive intmax_t
values.
(duration::__divide): New helper alias for dividing one period
by another.
(duration::__is_harmonic): Use __divide not ratio_divide.
(duration(const duration<R2, P2>&)): Require the duration rep
types to be convertible.
* testsuite/20_util/duration/cons/dr2094.cc: New test.
* testsuite/20_util/duration/requirements/reduced_period.cc:
Fix definition of unused member functions in test type.
* testsuite/20_util/duration/requirements/typedefs_neg2.cc:
Adjust expected errors.
libstdc++-v3/include/std/chrono
libstdc++-v3/testsuite/20_util/duration/cons/dr2094.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc
libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg2.cc