On 27/08/20 22:41 +0100, Jonathan Wakely wrote:
This implements the changes from P0548 "common_type and duration". That
was a change for C++17, but as it corrects some issues introduced by DRs
I'm also treating it as a DR and changing it for all modes from C++11
up.
The main change is that duration<R,P>::period no longer denotes P, but
rather P::type, the reduced ratio. The unary operator+ and operator-
members of duration should now return a duration using that reduced
ratio.
The requirement that common_type<T>::type is the same type as
common_type<T, T>::type (rather than simply T) was already implemented
for PR 89102.
The standard says that duration::operator+() and duration::operator-()
should return common_type_t<duration>, but that seems unnecessarily
expensive to compute. This change just uses duration<rep, period> which
is the same type, so we don't need to instantiate common_type.
As an optimization, this also adds partial specializations of
common_type for two durations of the same type, a single duration, two
time_points of the same type, and a single time_point. These
specializations avoid instantiating other specializations of common_type
and one or both of __duration_common_type or __timepoint_common_type for
the cases where the answer is trivial to obtain.
libstdc++-v3/ChangeLog:
* include/std/chrono (__duration_common_type): Ensure the
reduced ratio is used. Remove unused partial specialization
using __failure_type.
(common_type): Pass reduced ratios to __duration_common_type.
Add partial specializations for simple cases involving a single
duration or time_point type.
(duration::period): Use reduced ratio.
(duration::operator+(), duration::operator-()): Return duration
type using the reduced ratio.
* testsuite/20_util/duration/requirements/typedefs_neg2.cc:
Adjust expected errors.
* testsuite/20_util/duration/requirements/reduced_period.cc: New test.
Tested powerpc64le-linux. Committed to trunk.
This is a C++17 feature, so I think it would be good to backport it to
gcc-10 as well. I'll let it bake on trunk for a while first though.
A correction ...
libstdc++: Fix common_type specializations for duration
My recent change to implement P0548 ("common_type and duration") was not
correct. The result of common_type_t<duration<R,P>, duration<R,P>>
should be duration<common_type_t<R>, P::type>, not duration<R, P::type>.
The common_type specialization for two different duration types was
correct, but the specializations for a single duration type (which only
exist to optimize compilation time) were wrong.
This fixes the partial specializations of common_type for a single
duration type, and also the return types of duration::operator+ and
duration::operator- which are supposed to use common_type_t<duration>.
libstdc++-v3/ChangeLog:
* include/std/chrono (common_type): Fix partial specializations
for a single duration type to use the common_type of the rep.
(duration::operator+, duration::operator-): Fix return types
to also use the common_type of the rep.
* testsuite/20_util/duration/requirements/reduced_period.cc:
Check duration using a rep that has common_type specialized.
Tested powerpc64le-linux. Committed to trunk.
commit f2f48b68a6a586f40dd8ae0e6d391b7f88756eec
Author: Jonathan Wakely <jwak...@redhat.com>
Date: Fri Aug 28 23:41:13 2020
libstdc++: Fix common_type specializations for duration
My recent change to implement P0548 ("common_type and duration") was not
correct. The result of common_type_t<duration<R,P>, duration<R,P>>
should be duration<common_type_t<R>, P::type>, not duration<R, P::type>.
The common_type specialization for two different duration types was
correct, but the specializations for a single duration type (which only
exist to optimize compilation time) were wrong.
This fixes the partial specializations of common_type for a single
duration type, and also the return types of duration::operator+ and
duration::operator- which are supposed to use common_type_t<duration>.
libstdc++-v3/ChangeLog:
* include/std/chrono (common_type): Fix partial specializations
for a single duration type to use the common_type of the rep.
(duration::operator+, duration::operator-): Fix return types
to also use the common_type of the rep.
* testsuite/20_util/duration/requirements/reduced_period.cc:
Check duration using a rep that has common_type specialized.
diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index fb251848da8..524d23ea6a7 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -114,13 +114,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Rep, typename _Period>
struct common_type<chrono::duration<_Rep, _Period>,
chrono::duration<_Rep, _Period>>
- { using type = chrono::duration<_Rep, typename _Period::type>; };
+ {
+ using type = chrono::duration<typename common_type<_Rep>::type,
+ typename _Period::type>;
+ };
/// Specialization of common_type for one chrono::duration type.
/// @relates duration
template<typename _Rep, typename _Period>
struct common_type<chrono::duration<_Rep, _Period>>
- { using type = chrono::duration<_Rep, typename _Period::type>; };
+ {
+ using type = chrono::duration<typename common_type<_Rep>::type,
+ typename _Period::type>;
+ };
// 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
@@ -463,13 +469,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// 20.11.5.3 arithmetic
- constexpr duration<rep, period>
+ constexpr duration<typename common_type<rep>::type, period>
operator+() const
- { return *this; }
+ { return duration<typename common_type<rep>::type, period>(__r); }
- constexpr duration<rep, period>
+ constexpr duration<typename common_type<rep>::type, period>
operator-() const
- { return duration(-__r); }
+ { return duration<typename common_type<rep>::type, period>(-__r); }
_GLIBCXX17_CONSTEXPR duration&
operator++()
diff --git a/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc b/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc
index 9eb38a0e56f..4c7472699d2 100644
--- a/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc
+++ b/libstdc++-v3/testsuite/20_util/duration/requirements/reduced_period.cc
@@ -129,3 +129,55 @@ test02()
static_assert( is_same<decltype(-d3), common_type<D3>::type>::value,
"unary - returns the reduced duration" );
}
+
+template<typename T>
+struct Number
+{
+ explicit
+ Number(T t = 0) : i(t)
+ { }
+
+ template<typename U, bool B = std::is_convertible<U, T>::value,
+ typename = typename std::enable_if<B>::type>
+ explicit
+ Number(Number<U> n) : i(n.i)
+ { }
+
+ T i = 0;
+
+ Number& operator+=(Number n) { i += n.i; return *this; }
+ Number& operator-=(Number n) { i -= n.i; return *this; }
+ Number& operator*=(Number n) { i *= n.i; return *this; }
+ Number& operator/=(Number n) { i /= n.i; return *this; }
+ Number& operator%=(Number n) { i %= n.i; return *this; }
+
+ Number operator+(Number n) { return { i + n.i }; }
+ Number operator-(Number n) { return { i - n.i }; }
+ Number operator*(Number n) { return { i * n.i }; }
+ Number operator/(Number n) { return { i / n.i }; }
+ Number operator%(Number n) { return { i % n.i }; }
+};
+
+namespace std
+{
+ // Specialise common_type to give a different type
+ template<>
+ struct common_type<Number<int>, Number<int>>
+ { using type = Number<long>; };
+}
+
+void
+test03()
+{
+
+ using D4 = duration<Number<int>, ratio<49, 21>>;
+ static_assert( is_same<common_type<D4>::type,
+ duration<Number<long>, ratio<7, 3>>>::value,
+ "common_type_t<duration<R,P>> uses common_type_t<R>" );
+
+ D4 d4;
+ static_assert( is_same<decltype(+d4), common_type<D4>::type>::value,
+ "unary + returns type with common_type_t<D4::rep> as rep" );
+ static_assert( is_same<decltype(-d4), common_type<D4>::type>::value,
+ "unary - returns type with common_type_t<D4::rep> as rep" );
+}