public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Iain D Sandoe <iains@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc/devel/c++-coroutines] libstdc++: Fix common_type specializations for duration Date: Mon, 31 Aug 2020 19:49:03 +0000 (GMT) [thread overview] Message-ID: <20200831194903.9E059385783D@sourceware.org> (raw) https://gcc.gnu.org/g:f2f48b68a6a586f40dd8ae0e6d391b7f88756eec commit f2f48b68a6a586f40dd8ae0e6d391b7f88756eec Author: Jonathan Wakely <jwakely@redhat.com> Date: Fri Aug 28 23:41:13 2020 +0100 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: --- libstdc++-v3/include/std/chrono | 18 +++++--- .../duration/requirements/reduced_period.cc | 52 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 6 deletions(-) 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" ); +}
reply other threads:[~2020-08-31 19:49 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20200831194903.9E059385783D@sourceware.org \ --to=iains@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).